--[[
FilePath: \Lua\View\UISoloBattle\UISoloBattle.lua
1v1战斗界面
--]]

require 'View/UIBase/UIBase'

UISoloBattle = UIBase.New(PanelNames.UISoloBattle)
local _M = UISoloBattle

_M.hasInit = false
_M.mapPre = nil;
_M.mapPreClone = nil;
_M.playRole = nil;
_M.enemyRole = nil;
_M.enemyRole2 = nil;
_M.isLoad = false;
_M.isPlay = false;
_M.isAutoBattle = false;  --不进战斗
_M.battlePartnerId = "";
_M.isBattle = false;
_M.isEndsInDraw = false;
_M.POTION_REVIVE = 200621; --复活药水
_M.POTION_ENERGY = 200622; --能量药水
_M.POTION_HP = 200623;  --回复药水
_M.isPlayerOperation = true;  --是否玩家点击战斗


function _M.Init(func)
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self.btnClose = self:GetChild("Btn_Back");

        self.map = self:GetChild("Map");
        self.mapNode = self.map:GetChild("MapNode");
        self.btnHero = self:GetChild("Btn_Hero");
        self.heroNode = self.btnHero:GetChild("Graph_Hero");
        self.buzhenEffectNode = self.btnHero:GetChild("Graph_fxh104");
        self.heroName = self.btnHero:GetChild("Text_HeroName");
        self.barBlood = self.btnHero:GetChild("Bar_Blood");
        self.barEnergy = self.btnHero:GetChild("Bar_Energy");
        self.btnEnemy = self:GetChild("Btn_Enemy");
        self.enemyNode = self.btnEnemy:GetChild("Graph_Enemy");
        self.btnNextEnemy = self:GetChild("Btn_NextEnemy");
        self.nextEnemyNode = self.btnNextEnemy:GetChild("Graph_Enemy");
        self.btnEnemy2 = self:GetChild("Btn_Enemy2");
        self.enemyNode2 = self.btnEnemy2:GetChild("Graph_Enemy");
        self.btnNextEnemy2 = self:GetChild("Btn_NextEnemy2");
        self.enemyEffect2 = self.btnEnemy2:GetChild("Graph_fxh104");
        self.attEff = self:GetChild("Btn_AttEff");
        self.attEffNode = self.attEff:GetChild("Graph_Enemy");
        self.hitEff = self:GetChild("Btn_HitEff");
        self.hitEffNode = self.hitEff:GetChild("Graph_Enemy");
        self.shopNode = self:GetChild("Graph_ShopDrop");
        self.buffNode = self:GetChild("Graph_BuffDrop");
        self.item = self:GetChild("Btn_PotionDrop");

        self.title = self:GetChild("Com_Title"):GetChild("title");
        self.textLevel = self:GetChild("Text_Level");
        self.textCountDown = self:GetChild("Text_CountDown");
        self.btnHelp = self:GetChild("Btn_Help");
        self.btnRank = self:GetChild("Btn_Rank");
        self.btnShop = self:GetChild("Btn_Shop");

        self.barAtk = self:GetChild("Bar_Atk");
        self.atkNum = self.barAtk:GetChild("Text_Num");
        self.barDef = self:GetChild("Bar_Def");
        self.defNum = self.barDef:GetChild("Text_Num");
        self.barHp = self:GetChild("Bar_Hp");
        self.hpNum = self.barHp:GetChild("Text_Num");
        self.btnJumpBattle = self:GetChild("Btn_JumpBattle");
        self.skip = self:GetController("Skip");
        self.btnPotion01 = self:GetChild("Btn_Potion01");
        self.potionNum01 = self.btnPotion01:GetChild("Text_Num");
        self.btnPotion02 = self:GetChild("Btn_Potion02");
        self.potionNum02 = self.btnPotion02:GetChild("Text_Num");
        self.btnPotion03 = self:GetChild("Btn_Potion03");
        self.potionNum03 = self.btnPotion03:GetChild("Text_Num");
        self.btnBattle = self:GetChild("Btn_Battle");
        self.listHero = self:GetChild("List_Hero");

        self.boxEffect = self:GetTransition("Effect_ChapterBoxDrop");
        self.boxEffect.invalidateBatchingEveryFrame = true;
        self.potionDrop1 = self:GetTransition("Effect_PotionDrop01");
        self.potionDrop1.invalidateBatchingEveryFrame = true;
        self.potionDrop2 = self:GetTransition("Effect_PotionDrop02");
        self.potionDrop2.invalidateBatchingEveryFrame = true;
        self.potionDrop3 = self:GetTransition("Effect_PotionDrop03");
        self.potionDrop3.invalidateBatchingEveryFrame = true;
        self.buffDrop1 = self:GetTransition("Effect_BuffDrop01");
        self.buffDrop1.invalidateBatchingEveryFrame = true;
        self.buffDrop2 = self:GetTransition("Effect_BuffDrop02");
        self.buffDrop2.invalidateBatchingEveryFrame = true;
        self.buffDrop3 = self:GetTransition("Effect_BuffDrop03");
        self.buffDrop3.invalidateBatchingEveryFrame = true;
        self.textShow = self:GetTransition("Effect_TextShow");
        self.textShow.ignoreEngineTimeScale = false;
        self.textShow.invalidateBatchingEveryFrame = true;
        self.resetEff = self:GetTransition("Effect_Reset");
        self.resetEff.invalidateBatchingEveryFrame = true;

        self.autoCtrl = self:GetTransition("auto");
        self.autoCtrl.ignoreEngineTimeScale = false;
        self.autoCtrl.invalidateBatchingEveryFrame = true;

        self.listEnemyHero = self:GetChild("List_EnemyHero");

        self.typeCtrl = self:GetController("Type");
        self.typeCtrl.selectedIndex = 0;

        self.listHero.itemRenderer = self.refreshSelectItem;

        self.listEnemyHero.itemRenderer = self.refreshItem;
        --self.listEnemyHero:SetVirtual();
        self.mainReward = self:GetChild("mainReward")
        logicMgr.FuncOpenManager.refreshMainReward(self.mainReward,Config.FuncOpenType.RealMan)

        self.btnAuto = self:GetChild("auto");
        self.autoFightCtrl = self:GetController("autoFight");

        -- 里程碑奖励
        self.txtTaskDesc = self:GetChild("taskDesc")
        self.listTaskRewards = self:GetChild("listReward1")
        self:initTaskRewardDatas()

        _M.hasInit = true;

        self:addEvent();
    end
    self:initView();
end

function _M:addEvent()
    self.btnClose.onClick:Add(self.onClosePanel,self);
    self.btnHero.onClick:Add(self.onBtnHero, self);
    self.btnEnemy.onClick:Add(self.onBtnEnemy, self);
    self.btnNextEnemy.onClick:Add(self.onBtnNextEnemy, self);
    self.btnHelp.onClick:Add(self.onBtnHelp, self);
    self.btnRank.onClick:Add(self.onBtnRank, self);
    self.btnShop.onClick:Add(self.onBtnShop, self);
    self.btnJumpBattle.onClick:Add(self.onBtnJumpBattle, self);
    self.btnPotion01.onClick:Add(self.onBtnPotion01, self);
    self.btnPotion02.onClick:Add(self.onBtnPotion02, self);
    self.btnPotion03.onClick:Add(self.onBtnPotion03, self);
    self.btnBattle.onClick:Add(self.onBtnBattle, self);
    self.btnAuto.onClick:Add(self.onBtnAuto, self);
end

function _M:addLocalEvent()
    self:RegisterEvent(Config.EventType.Fresh_RealManBattle,self.realManBattle);
    self:RegisterEvent(Config.EventType.Fresh_RealManUseItem,self.realManBattleUseItem);
    self:RegisterEvent(Config.EventType.Fresh_RealManRealManWar,self.closeRealManBattle);
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UISoloBattle,Config.FuncOpenType.RealManShop, self.btnShop.name,self.btnShop);
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UISoloBattle,Config.FuncOpenType.autoAttack, self.btnAuto.name,self.btnAuto);
end

function _M:removeEvent()
    self.btnClose.onClick:Remove(self.onClosePanel);
    self.btnHero.onClick:Remove(self.onBtnHero);
    self.btnEnemy.onClick:Remove(self.onBtnEnemy);
    self.btnNextEnemy.onClick:Remove(self.onBtnNextEnemy);
    self.btnHelp.onClick:Remove(self.onBtnHelp);
    self.btnRank.onClick:Remove(self.onBtnRank);
    self.btnShop.onClick:Remove(self.onBtnShop);
    self.btnJumpBattle.onClick:Remove(self.onBtnJumpBattle);
    self.btnPotion01.onClick:Remove(self.onBtnPotion01);
    self.btnPotion02.onClick:Remove(self.onBtnPotion02);
    self.btnPotion03.onClick:Remove(self.onBtnPotion03);
    self.btnBattle.onClick:Remove(self.onBtnBattle);
    self.btnAuto.onClick:Remove(self.onBtnAuto);
end

function _M:removeLocalEvent()
    self:UnRegisterEvent(Config.EventType.Fresh_RealManBattle,self.realManBattle);
    self:UnRegisterEvent(Config.EventType.Fresh_RealManUseItem,self.realManBattleUseItem);
    self:UnRegisterEvent(Config.EventType.Fresh_RealManRealManWar,self.closeRealManBattle);
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UISoloBattle);
end

function _M:realManBattle(isSuc)
    if not isSuc then
        self.isBattle = false;
        return;
    end
    if self.isAutoBattle then
        self:autoBattle();
    else
        local battleResult = logicMgr.RealManWarManager.getFightResult();
        if battleResult ~= nil and battleResult.record ~= nil then
            --logicMgr.BattlePlaybackManager.ResetReceiveReport()
            --local mapCfg = MapManager.getMapcfg();
            --if  string.IsNilOrEmpty(mapCfg.f_ReportAddress) then
            --    logicMgr.BattlePlaybackManager.GotoFight(EnumConst.FightTypeConst.RealMan);
            --    logicMgr.RealManWarManager.setEnterBattle(true);
            --    BattlePlaybackManager.RequestReport(battleResult.fightId,battleResult.fightResult,battleResult.record);
            --else
            --    self:autoBattle();
            --end
            
            
            logicMgr.RealManWarManager.setEnterBattle(true);
            BattlePlaybackManager.setBattleRecord(battleResult.record);
            BattlePlaybackManager.setBattleResult(battleResult.fightResult);
            BattlePlaybackManager.requestFightCmdThenPlay(battleResult.record, EnumConst.FightTypeConst.RealMan);
            AudioManager.Play(29)
            --logError(">>>>>>>>>>>")
        else
            self:autoBattle();
        end
    end
end

function _M:realManBattleUseItem(isSuc, itemId)
    if not isSuc then
        return;
    end
    self.realManPlayerInfo = logicMgr.RealManWarManager.GetRealManPlayerInfo();
    if itemId == self.POTION_REVIVE then
        local needLoadHero = false;
        if self.battlePartnerId == nil or self.battlePartnerId == "" or self.battlePartnerId == "noHero" then
            needLoadHero = true;
        else
            local parHero = self.realManPlayerInfo.myTeam[self.battlePartnerId];
            if parHero ~= nil and parHero.hp == 0 then
                needLoadHero = true;
            end
        end
        if needLoadHero then
            self:setSurvivalHero();
            if self.modelId ~= nil or self.modelId ~= "" then
                self.loadNum = 0;
                self:loadHero(self.modelId);
                self.typeCtrl.selectedIndex = 0;
            else
                self.typeCtrl.selectedIndex = 1;
            end
        end
    else
        self:refreshBattleHeroState();
    end
    self.listHero.numItems = #self.realManPlayerInfo.formation;
    local potion = self.realManPlayerInfo.potion;
    if potion ~= nil then
        local num = potion[self.POTION_REVIVE];
        if num ~= nil then
            self.potionNum01.text = tonumber(num);
        end
        num = potion[self.POTION_ENERGY];
        if num ~= nil then
            self.potionNum02.text = tonumber(num);
        end
        num = potion[self.POTION_HP];
        if num ~= nil then
            self.potionNum03.text = tonumber(num);
        end
    end
end

function _M:autoBattle()
    self.realManPlayerInfo = logicMgr.RealManWarManager.GetRealManPlayerInfo();
    self.battleResult = logicMgr.RealManWarManager.getFightResult();

    self.curPosIndex = logicMgr.RealManWarManager.getPosIndex();
    if self.curPosIndex > self.posMaxIndex then
        self.curPosIndex = 1;
    end
    if self.curPosIndex == 1 and self.battleMapCfg ~= nil then
        self.MapPos = decodeJsonStrToTabel(self.battleMapCfg.f_MapPos);
        self.map:SetXY(self.MapPos[self.curPosIndex][1], self.MapPos[self.curPosIndex][2]);
    end
    --self:refreshBattleInfo();
    _M:removeTimeCall();
    self:updateHeroHP();
    self.loadNum = 3;
    self:updateEnemyInfo(true);
    self.enemyDeadIndex = 0;
    if self.battleResult ~= nil then
        if self.enemyHeros[2] ~= nil then
            if self.enemyHeros[2].deadType == 0 then
                self.enemyDeadIndex = 2;
            end
        end
        if self.enemyHeros[1] ~= nil then
            if self.enemyHeros[1].deadType == 0 then
                self.enemyDeadIndex = 1;
            end
        end
    end

    self:loadEnd();
    --if self.battleResult ~= nil and self.battleResult.fightResult ~= 1 then
    self.listHero.numItems = #self.realManPlayerInfo.formation;
    --end
    --logicMgr.RealManWarManager.setFightResult(nil);
    self.isBattle = false;
end

function _M:onBtnHero()
    if self.isPlay then
        return;
    end
    if self.isBattle then
        return;
    end
end

function _M:onBtnEnemy()
    if self.isPlay then
        return;
    end
    if self.isBattle then
        return;
    end
end

function _M:onBtnNextEnemy()
    if self.isPlay then
        return;
    end
    if self.isBattle then
        return;
    end
end

function _M:onBtnHelp()
    if self.isBattle then
        return;
    end
    logicMgr.UIShowManager.ShowCommonTips("SoloBattle_Info_Title", "SoloBattle_Info_Tips", "FunctionStory311");
end

function _M:onBtnRank()
    if self.isPlay then
        return;
    end
    if self.isBattle then
        return;
    end
    showUI(PanelNames.UISoloBattleRank);
end

function _M:onBtnShop()
    if self.isPlay then
        return;
    end
    if self.isBattle then
        return;
    end
    --showUI(PanelNames.UISoloShop);
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.RealManShop, true, self.btnShop) then
        showUI(PanelNames.UIShop, EnumConst.ShopType.mythicShop);
    end
end

function _M:onBtnAuto()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.autoAttack, true, self.btnAuto) then
        if self.autoFightCtrl.selectedIndex == 0 then
            FightManager.isAuto = true;
            logicMgr.RealManWarManager.setPlayerOperationState(true);
            self.isPlayerOperation = false;
            self.autoFightCtrl.selectedIndex = 1;
            self:onBtnBattle();
        else
            FightManager.isAuto = nil;
            logicMgr.RealManWarManager.setPlayerOperationState(false);
            self.isPlayerOperation = true;
            self.autoFightCtrl.selectedIndex = 0;
        end
    end
end

function _M:onBtnJumpBattle()
    self.isAutoBattle = self.btnJumpBattle.selected;
    logicMgr.RealManWarManager.setAutoBattleState(self.isAutoBattle);
    if self.isAutoBattle and self.mapPreClone == nil then
        self:loadLoopScene();
    end
end

function _M:onBtnPotion01()
    if self.isPlay then
        return;
    end
    if self.isBattle then
        return;
    end
    showUI(PanelNames.UIUsingPotion, nil, self.POTION_REVIVE, self.battlePartnerId);
end

function _M:onBtnPotion02()
    if self.isPlay then
        return;
    end
    if self.isBattle then
        return;
    end
    showUI(PanelNames.UIUsingPotion, nil, self.POTION_ENERGY, self.battlePartnerId);
end

function _M:onBtnPotion03()
    if self.isPlay then
        return;
    end
    if self.isBattle then
        return;
    end
    showUI(PanelNames.UIUsingPotion, nil, self.POTION_HP, self.battlePartnerId);
end

function _M:onBtnBattle()
    if not logicMgr.RealManWarManager.isOpen() then
        UISysTips.AddMsg(getLanguage("ErrorCode_200007"));
        return;
    end
    if self.isPlay then
        return;
    end
    if self.isBattle then
        return;
    end
    if self.battlePartnerId == "" or self.battlePartnerId == "0" then
        return;
    end
    if self.battlePartnerId == "noHero" then
        UISysTips.AddMsg(getLanguage("ErrorCode_200003"));
        return;
    end
    local myTeamNum = #self.realManPlayerInfo.myTeam;
    if #self.realManPlayerInfo.formation <= myTeamNum then
        local hasSurvival = false;
        for i = 1, myTeamNum do
            local item = self.realManPlayerInfo.myTeam[i];
            if item.hp > 0 then
                hasSurvival = true;
                break;
            end
        end
        if not hasSurvival then
            UISysTips.AddMsg(getLanguage("ErrorCode_200003"));
            return;
        end
    end
    if self.enemyModelId == nil or self.enemyModelId == "" then
        UISysTips.AddMsg(getLanguage("ErrorCode_200006"));
        return;
    end

    self.isBattle = true;
    local formation = {};
    table.insert(formation, self.battlePartnerId);
    for i = 1, 5 do
        table.insert(formation, "0");
    end

    BattlePlaybackManager.SetFightType(EnumConst.FightTypeConst.RealMan);
    RealManWarHandler.sendReqHappyFuckMessage(formation, self.isAutoBattle);
end

function _M:initView()
    self.battleMapCfg = G.dataTable["t_solobattlemap"][tostring("1")];
    self.buffTypeCfg = G.dataTable["t_abyssbufftype"];
    self.curPosIndex = logicMgr.RealManWarManager.getPosIndex();
    self.isAutoBattle = logicMgr.RealManWarManager.getAutoBattleState();
    self.btnJumpBattle.selected = self.isAutoBattle;
    self.posMaxIndex = 1;
    self.speed = 30;
    self.mapScale = 100;
    self.firMapPos = {0,0,0};
    self.secMapPos = {4660,0,0};
    self.battleMapPos = {-2.35, -10.4, -1.71};
    if self.battleMapCfg ~= nil then
        self.MapPos = decodeJsonStrToTabel(self.battleMapCfg.f_MapPos);
        self.posMaxIndex = #self.MapPos - 1;
        logicMgr.RealManWarManager.setMaxPosIndex(self.posMaxIndex);
        self.speed = tonumber(self.battleMapCfg.f_Speed);
        self.mapScale = tonumber(self.battleMapCfg.f_MapScale);
        self.firMapPos = decodeJsonStrToTabel(self.battleMapCfg.f_FirMapPos);
        self.secMapPos = decodeJsonStrToTabel(self.battleMapCfg.f_SecMapPos);
        self.battleMapPos = decodeJsonStrToTabel(self.battleMapCfg.f_BattleMapPos);
        if self.curPosIndex > self.posMaxIndex then
            self.curPosIndex = 1;
        end
        if self.battleMapCfg.f_MapPosSpacing ~= nil and self.battleMapCfg.f_MapPosSpacing ~= "" then
            self.btnNextEnemy:SetXY(tonumber(self.battleMapCfg.f_MapPosSpacing) + self.btnEnemy.x, self.btnEnemy.y);
            self.btnNextEnemy2:SetXY(tonumber(self.battleMapCfg.f_MapPosSpacing) + self.btnEnemy2.x, self.btnEnemy2.y);
        end
        self.map:SetXY(self.MapPos[self.curPosIndex][1], self.MapPos[self.curPosIndex][2]);
    end

    self.loadNum = 0;
    self.heroMovePos = Vector3.New(0, 0, 0);
    self.heroMovePos:Set(self.btnEnemy.x, self.btnEnemy.y);
    self.heroMovePos2 = Vector3.New(0, 0, 0);
    self.heroMovePos2:Set(self.btnEnemy2.x, self.btnEnemy2.y);
    self.bloodBarPos = {self.barBlood.x, self.barBlood.y};
    self.energyPos = {self.barEnergy.x, self.barEnergy.y};
    self.heroNamePos = {self.heroName.x, self.heroName.y};

    UIComponentInterface:SetItemAllById(self.btnPotion01:GetChild("Loader_Type"), nil, nil, self.POTION_REVIVE);
    UIComponentInterface:SetItemAllById(self.btnPotion02:GetChild("Loader_Type"), nil, nil, self.POTION_ENERGY);
    UIComponentInterface:SetItemAllById(self.btnPotion03:GetChild("Loader_Type"), nil, nil, self.POTION_HP);
    --logicMgr.RealManWarManager.loadScene();

    FightManager.isAuto = nil;
    FightManager.battleFunction = nil;
    self.autoFightCtrl.selectedIndex = 0;
    self.isPlayerOperation = true;
    logicMgr.RealManWarManager.setPlayerOperationState(false);
    --if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.autoAttack) then
    --    local autoState = GetPlayerPrefs("UISoloBattleAuto" .. logicMgr.HeroManager.GetRoleInfo("playerId"));
    --    if autoState ~= nil and tonumber(autoState) == 1 then
    --        FightManager.isAuto = true;
    --        logicMgr.RealManWarManager.setPlayerOperationState(true);
    --        self.isPlayerOperation = false;
    --        self.autoFightCtrl.selectedIndex = 1;
    --    end
    --end

    self:updateData();
end

function _M:updateData()
    self.realManPlayerInfo = logicMgr.RealManWarManager.GetRealManPlayerInfo();
    self.battleResult = logicMgr.RealManWarManager.getFightResult();
    if self.realManPlayerInfo == nil or self.realManPlayerInfo.formation == nil or #self.realManPlayerInfo.formation < 1 then
        self:onClosePanel();
    end
    self:refreshBattleInfo();

    self.selectedHero = {};
    self.battleFormation = self.realManPlayerInfo.formation;
    local battleNum = #self.battleFormation;
    self.battlePartnerId = self.realManPlayerInfo.partnerId;
    for i = 1, battleNum do
        local parId = self.battleFormation[i];
        local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(parId, false);
        if hero ~= nil then
            self.selectedHero[parId] = hero;
            if self.battlePartnerId == "" or self.battlePartnerId == "0" then
                self.battlePartnerId = parId;
                self.modelId = hero.partnerModelId;
            elseif self.battlePartnerId == hero.partnerId then
                self.modelId = hero.partnerModelId;
            end
        end
    end

    if self.battleResult == nil then
        local parHero = self.realManPlayerInfo.myTeam[self.battlePartnerId];
        if parHero ~= nil and parHero.hp == 0 then
            --判断是否有英雄存活
            self:setSurvivalHero();
        end
    end

    self.listHero.numItems = battleNum;

    self:hideHeroInfo();
    self:loadScene();
    if self.modelId ~= nil and self.modelId ~= "" then
        self:loadHero(self.modelId);
    else
        --没有存活了
        self.battlePartnerId = "noHero";
        --判断是否有复活药水
        if self.realManPlayerInfo.potion ~= nil and self.realManPlayerInfo.potion[self.POTION_REVIVE] ~= nil and self.realManPlayerInfo.potion[self.POTION_REVIVE] > 0 then
            showUI(PanelNames.UIUsingPotion, nil, self.POTION_REVIVE);
        else
            --全部英雄阵亡
            self.typeCtrl.selectedIndex = 1;
        end
        self:loadEnd();
    end
    local cfgId;
    if self.battleResult ~= nil then
        cfgId = self.battleResult.step;
    else
        cfgId = tonumber(self.realManPlayerInfo.step) + 1;
    end
    self.abyssCfg = G.dataTable["t_abyss"][tostring(cfgId)];
    if self.abyssCfg ~= nil then
        self.enemyModelId = tostring(self.abyssCfg.f_HeroID);
        self.textLevel.text = string.format(getLanguage("SoloBattle_Level"), getLanguage("SoloBattle_difficulty0"..self.abyssCfg.f_Rank),self.abyssCfg.f_RankLevel);
    else
        self.textLevel.text = "";
    end
    if self.enemyModelId ~= nil and self.enemyModelId ~= "" then
        self:showTaskInfo(cfgId)
        self:updateEnemyInfo(true);
        self.enemyDeadIndex = 0;
        if self.battleResult ~= nil then
            local serverArmyTeam = self.realManPlayerInfo.armyTeam;
            local serverPreArmyTeam = self.realManPlayerInfo.preArmyTeam;
            local armyTeamNum = 0;
            local preArmyTeamNum = 0;
            if serverArmyTeam ~= nil then
                for k,v in pairs(serverArmyTeam) do
                    armyTeamNum = armyTeamNum + 1;
                end
            end
            if serverPreArmyTeam ~= nil then
                for k,v in pairs(serverPreArmyTeam) do
                    preArmyTeamNum = preArmyTeamNum + 1;
                end
            end
            if (serverArmyTeam == nil or armyTeamNum < 1) and (serverPreArmyTeam == nil or preArmyTeamNum < 1) then
                if self.enemyHeros[2] ~= nil then
                    self:loadEnemyHero2(self.enemyHeros[2].modelId, false);
                    if self.enemyHeros[2].deadType == 0 then
                        self.enemyDeadIndex = 2;
                    end
                end
                if self.enemyHeros[1] ~= nil then
                    self:loadEnemyHero(self.enemyHeros[1].modelId, false);
                    if self.enemyHeros[1].deadType == 0 then
                        self.enemyDeadIndex = 1;
                    end
                end
            else
                local curArmyTeam = {};
                if serverArmyTeam ~= nil and armyTeamNum > 0 then
                    curArmyTeam = serverArmyTeam;
                else
                    if serverPreArmyTeam ~= nil then
                        curArmyTeam = serverPreArmyTeam;
                    end
                end

                if self.enemyHeros[1] ~= nil then
                    local team = curArmyTeam[self.enemyHeros[1].partnerId];
                    if team ~= nil and team.needLoadHero then
                        if self.enemyHeros[2] ~= nil then
                            local team2 = curArmyTeam[self.enemyHeros[2].partnerId];
                            if team2 ~= nil and team2.needLoadHero then
                                self:loadEnemyHero2(self.enemyHeros[2].modelId, false);
                                if self.enemyHeros[2].deadType == 0 then
                                    self.enemyDeadIndex = 2;
                                end
                            end
                        end
                        self:loadEnemyHero(self.enemyHeros[1].modelId, false);
                        if self.enemyHeros[1].deadType == 0 then
                            self.enemyDeadIndex = 1;
                        end
                    else
                        if self.enemyHeros[2] ~= nil then
                            local team2 = curArmyTeam[self.enemyHeros[2].partnerId];
                            if team2 ~= nil and team2.needLoadHero then
                                self:loadEnemyHero(self.enemyHeros[2].modelId, false);
                                if self.enemyHeros[2].deadType == 0 then
                                    self.enemyDeadIndex = 2;
                                end
                            end
                        end
                    end
                else
                    if self.enemyHeros[2] ~= nil and self.enemyHeros[2].deadType == 1 then
                        self:loadEnemyHero(self.enemyHeros[2].modelId, false);
                    end
                end
            end
        else
            if self.enemyHeros[1] ~= nil then
                if self.enemyHeros[1].deadType == 1 then
                    if self.enemyHeros[2] ~= nil and self.enemyHeros[2].deadType == 1 then
                        self:loadEnemyHero2(self.enemyHeros[2].modelId, false);
                    end
                    self:loadEnemyHero(self.enemyHeros[1].modelId, false);
                else
                    if self.enemyHeros[2] ~= nil and self.enemyHeros[2].deadType == 1 then
                        self:loadEnemyHero(self.enemyHeros[2].modelId, false);
                    end
                end
            end
        end
    else
        --打穿了;
        self.textLevel.text = "";
        self:loadEnd();
        self.typeCtrl.selectedIndex = 2;
        self:hideTaskInfo()
    end
    if (tonumber(self.realManPlayerInfo.step) + 1) > 20 then
        self.skip.selectedIndex = 1;
    else
        self.skip.selectedIndex = 0;
    end
    --logicMgr.RealManWarManager.setFightResult(nil);

    local realManWarInfo = logicMgr.RealManWarManager.getRealManWarInfo();
    if realManWarInfo ~= nil then
        local time = HelperFunc.GetServerTime();
        self.endCountDown = realManWarInfo.nextChangeStatusTime - time;
        if realManWarInfo.status == 0 then
            self.endCountDown = 0;
        end
        if self.endCountDown > 0 then
            self:TimeCall(self.updateCountDown, 1000, -1);
        end
        self:updateCountDown();
    end
end

function _M:updateCountDown()
    if self.endCountDown == nil then
        return;
    end
    if self.endCountDown <= 0 then
        self.textCountDown.text = getLanguage("ErrorCode_200007");
        self:RemoveTimeCall(self.updateCountDown);
        self.typeCtrl.selectedIndex = 3;
    else
        self.textCountDown.text = string.format(getLanguage("SoloBattle_CountDown"), timeToTimeFormat(self.endCountDown));
    end
    self.endCountDown = self.endCountDown - 1;
end

function _M:refreshBattleInfo()
    local buff, potion;
    if self.battleResult ~= nil then
        if self.realManPlayerInfo.preBuff ~= nil then
            buff = self.realManPlayerInfo.preBuff;
        else
            buff = {};
        end
        if self.realManPlayerInfo.prePotion ~= nil then
            potion = self.realManPlayerInfo.prePotion;
        else
            potion = {};
        end
    else
        buff = self.realManPlayerInfo.buff;
        potion = self.realManPlayerInfo.potion;
    end
    self:setProgressValue(self.barAtk, self.atkNum, 1, 0, 30, 2);
    self:setProgressValue(self.barDef, self.defNum, 2, 0, 30, 6);
    self:setProgressValue(self.barHp, self.hpNum, 3, 0, 30, 3);
    if buff ~= nil then
        local num = buff[1];
        if num ~= nil then
            self:setProgressValue(self.barAtk, self.atkNum,1, num, 30, 2);
        end
        num = buff[2];
        if num ~= nil then
            self:setProgressValue(self.barDef, self.defNum, 2, num, 30, 6);
        end
        num = buff[3];
        if num ~= nil then
            self:setProgressValue(self.barHp, self.hpNum, 3, num, 30, 3);
        end
    end
    self.potionNum01.text = 0;
    self.potionNum02.text = 0;
    self.potionNum03.text = 0;
    if potion ~= nil then
        local num = potion[self.POTION_REVIVE];
        if num ~= nil then
            self.potionNum01.text = tonumber(num);
        end
        num = potion[self.POTION_ENERGY];
        if num ~= nil then
            self.potionNum02.text = tonumber(num);
        end
        num = potion[self.POTION_HP];
        if num ~= nil then
            self.potionNum03.text = tonumber(num);
        end
    end
end

function _M:setProgressValue(bar, txtBar, type, num, maxNum, scale)
    local cfg;
    if self.buffTypeCfg ~= nil then
        cfg = self.buffTypeCfg[tostring(type)];
    end
    if cfg ~= nil then
        maxNum = tonumber(cfg.f_MaxBuffLayer);
        scale = tonumber(cfg.f_BuffEffectShow);
    end
    local value = math.min(num / maxNum * 100, 100);
    bar.value = value;
    txtBar.text = "+"..(num * scale).."%";
end

function _M.refreshSelectItem(index, itemObj)
    local parId = _M.battleFormation[index + 1];
    local hero = _M.selectedHero[parId];
    if hero == nil then
        itemObj.visible = false;
        return ;
    end
    local charactor = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    if charactor ~= nil then
        charactor.initData(itemObj, hero);
        charactor.updateDecTitle(itemObj, hero, 0, nil, EnumConst.FightTypeConst.RealMan);
    end
    local ctrlHadBattle = itemObj:GetController("ctrlHadBattle");
    local herodead = itemObj:GetController("herodead");
    local ctrlBar = itemObj:GetController("Bar");
    local hpBar = itemObj:GetChild("BloodBar");
    local mpBar = itemObj:GetChild("EnergyBar");

    ctrlHadBattle.selectedIndex = 0;
    itemObj.onClick:Clear();
    local isDead = false;
    local hp = 100;
    local mp = 100;
    herodead.selectedIndex = 0;
    local myHero = _M.realManPlayerInfo.myTeam[parId];
    if myHero ~= nil and myHero.hp == 0 then
        herodead.selectedIndex = 1;
        isDead = true;
        hp = 0;
        mp = 0;
    else
        if parId == _M.battlePartnerId then
            ctrlHadBattle.selectedIndex = 1;
            _M.ctrlHadBattle = ctrlHadBattle;
        else
            ctrlHadBattle.selectedIndex = 0;
        end
        if myHero == nil then
            hp = 100;
            mp = 100;
        else
            hp = math.min(myHero.hp / myHero.maxHp * 100, 100);
            mp = math.min(tonumber(myHero.energy) / 1000 * 100, 100);
        end
    end

    ctrlBar.selectedIndex = 1
    hpBar.value = hp
    mpBar.value = mp
    itemObj.onClick:Add(function()
        if _M.isLoad or _M.isPlay then
            return ;
        end
        if isDead then
            if _M.realManPlayerInfo.potion ~= nil and _M.realManPlayerInfo.potion[_M.POTION_REVIVE] ~= nil and _M.realManPlayerInfo.potion[_M.POTION_REVIVE] > 0 then
                showUI(PanelNames.UIUsingPotion, nil, _M.POTION_REVIVE);
            else
                UISysTips.AddMsg(getLanguage("ErrorCode_200001"));
            end
        elseif parId ~= _M.battlePartnerId then
            _M.battlePartnerId = parId;
            _M.modelId = hero.partnerModelId;
            _M.loadNum = 0;
            _M:loadHero(_M.modelId);
            _M:playbuzhenEffect();
            if _M.ctrlHadBattle ~= nil then
                _M.ctrlHadBattle.selectedIndex = 0;
                _M.ctrlHadBattle = ctrlHadBattle;
                _M.ctrlHadBattle.selectedIndex = 1;
            end
        end
    end);
end

-- 加载地图
function _M:loadScene()
    if self.mapPre == nil then
        resMgr:LoadMapPrefab("Prefabs/MapPref/"..self.battleMapCfg.f_Map, function(map)
            self.mapPre = newObject(map);
            Util.SetLayerMask(self.mapPre,"UI",true);
            local mapObj = Util.Lua_FindGameObject(self.mapPre, "Map")
            local battleMapObj = Util.Lua_FindGameObject(self.mapPre, "BattleMap")
            if mapObj ~= nil then
                mapObj:SetActive(false);
            end
            if self.mapWrapper == nil then
                self.mapWrapper = GoWrapper.New();
            end
            self.mapWrapper:SetWrapTarget(self.mapPre,false);
            self.mapNode:SetNativeObject(self.mapWrapper);
            Util.Lua_SetLocalPositionXYZ(self.mapPre, self.firMapPos[1], self.firMapPos[2], self.firMapPos[3]);
            if battleMapObj ~= nil then
                Util.Lua_SetLocalPositionXYZ(battleMapObj, self.battleMapPos[1], self.battleMapPos[2], self.battleMapPos[3]);
            end
            Util.Lua_SetLocalRotationXYZ(self.mapPre, 0, 0, 0);
            Util.Lua_SetLocalScaleXYZ(self.mapPre, self.mapScale, self.mapScale, self.mapScale);
            if self.curPosIndex >= (self.posMaxIndex - 1) or self.isAutoBattle then
                self.mapPreClone = newObject(map);
                Util.SetLayerMask(self.mapPreClone,"UI",true);
                local mapObjC = Util.Lua_FindGameObject(self.mapPreClone, "Map")
                local battleMapObjC = Util.Lua_FindGameObject(self.mapPreClone, "BattleMap")
                if mapObjC ~= nil then
                    mapObjC:SetActive(false);
                end
                if self.mapWrapperC == nil then
                    self.mapWrapperC = GoWrapper.New();
                end
                self.mapWrapperC:SetWrapTarget(self.mapPreClone,false);
                self.mapNode:SetNativeObject(self.mapWrapperC);
                Util.Lua_SetLocalPositionXYZ(self.mapPreClone, self.secMapPos[1], self.secMapPos[2], self.secMapPos[3]);
                if battleMapObjC ~= nil then
                    Util.Lua_SetLocalPositionXYZ(battleMapObjC, self.battleMapPos[1], self.battleMapPos[2], self.battleMapPos[3]);
                end
                Util.Lua_SetLocalRotationXYZ(self.mapPreClone, 0, 0, 0);
                Util.Lua_SetLocalScaleXYZ(self.mapPreClone, self.mapScale, self.mapScale, self.mapScale);
            end
            self:loadEnd();
        end);
    else
        self:loadEnd();
    end
end

--加载循环地图
function _M:loadLoopScene()
    if self.mapPreClone == nil then
        resMgr:LoadMapPrefab("Prefabs/MapPref/" .. self.battleMapCfg.f_Map, function(map)
            self.mapPreClone = newObject(map);
            Util.SetLayerMask(self.mapPreClone, "UI", true);
            local mapObjC = Util.Lua_FindGameObject(self.mapPreClone, "Map")
            local battleMapObjC = Util.Lua_FindGameObject(self.mapPreClone, "BattleMap")
            if mapObjC ~= nil then
                mapObjC:SetActive(false);
            end
            if self.mapWrapperC == nil then
                self.mapWrapperC = GoWrapper.New();
            end
            self.mapWrapperC:SetWrapTarget(self.mapPreClone, false);
            self.mapNode:SetNativeObject(self.mapWrapperC);
            Util.Lua_SetLocalPositionXYZ(self.mapPreClone, self.secMapPos[1], self.secMapPos[2], self.secMapPos[3]);
            if battleMapObjC ~= nil then
                Util.Lua_SetLocalPositionXYZ(battleMapObjC, self.battleMapPos[1], self.battleMapPos[2], self.battleMapPos[3]);
            end
            Util.Lua_SetLocalRotationXYZ(self.mapPreClone, 0, 0, 0);
            Util.Lua_SetLocalScaleXYZ(self.mapPreClone, self.mapScale, self.mapScale, self.mapScale);
        end);
    end
end

-- 加载自己英雄
function _M:loadHero(modelId)
    --local useSkinId = logicMgr.SkinManager.getHeroSkinId(modelId)
    --local path = logicMgr.SkinManager.GetHeroModelPathBySkinId(useSkinId);
    --resMgr:LoadModelPrefab(path, function(go)
    --    if self.playRole ~= nil then
    --        destroyImmediate(self.playRole);
    --        self.playRole = nil
    --    end
    --    self.playRole = newObject(go);
    --    self.playRole:AddComponent(TCanvas)
    --    if self.heroWrapper == nil then
    --        self.heroWrapper = GoWrapper.New()
    --    end
    --    self.heroWrapper:SetWrapTarget(self.playRole,false);
    --    Util.Lua_SetLocalPositionXYZ(self.playRole, 100, -365, 0);
    --    Util.Lua_SetLocalScaleXYZ(self.playRole, 150, 150, 150);
    --    self.heroNode:SetNativeObject(self.heroWrapper);
    --    local shadow = Util.Lua_FindGameObject(self.playRole, "shadow");
    --    if shadow ~= nil then
    --        shadow.transform.localEulerAngles = Vector3.New(40, 0, 0);
    --    end
    --    self.heroObj = Util.Lua_FindGameObject(self.playRole, "Animation");
    --    Util.Lua_SetLocalRotationXYZ(self.heroObj, 0, 0, 0);
    --    --self.skeletonAnim = self.heroObj:GetComponent("SkeletonAnimation");
    --    logicMgr.EquipManager.setWeaponSkin(self.heroObj, modelId, true);
    --    Util.PlayAction(self.heroObj, self:getAnimationName(modelId,"idleBattle"));
    --    self:loadEnd();
    --    self:updateHeroHP(modelId);
    --end);
    if self.heroItem == nil then
        self.attEff:SetXY(self.btnHero.x, self.btnHero.y);
        self.heroItem = self.SoloBattleHeroItem.New();
        self.heroItem:OnCreate(self.btnHero,self.attEff,self);
    end
    self.heroItem:OnShow(modelId, false, true);
    self:updateHeroHP(modelId);
end

function _M:hideHeroInfo()
    self.heroName.visible = false;
    self.barBlood.visible = false;
    self.barEnergy.visible = false;
end

function _M:updateHeroHP(modelId)
    self.heroName.visible = true;
    self.barBlood.visible = true;
    self.barEnergy.visible = true;
    local hero = self.selectedHero[self.battlePartnerId];
    self.heroName.text = hero.name;
    local hpValue,enValue;
    if self.realManPlayerInfo.myTeam[self.battlePartnerId] == nil or self.realManPlayerInfo.myTeam[self.battlePartnerId].hp == -1 then
        hpValue = 100;
        enValue = 100;
    else
        local battleHero = self.realManPlayerInfo.myTeam[self.battlePartnerId];
        hpValue = math.min(battleHero.hp / battleHero.maxHp * 100, 100);
        enValue = math.min(tonumber(battleHero.energy) / 1000 * 100, 100);
    end
    self.barBlood.value = hpValue;
    self.barEnergy.value = enValue;
    local heroCfg = G.dataTable["t_hero"][tostring(modelId)];
    if heroCfg ~= nil then
        local roleCfg = G.dataTable["t_roleconfig"][tostring(heroCfg.f_HeroModel)];
        if roleCfg ~= nil and roleCfg.BloodBar_Position ~= nil then
            local pos = tonumber(roleCfg.BloodBar_Position) - 235;
            if pos > 0 then
                pos = pos * 2;
            end
            self.barBlood:SetXY(self.bloodBarPos[1], self.bloodBarPos[2] - pos);
            self.barEnergy:SetXY(self.energyPos[1], self.energyPos[2] - pos);
            self.heroName:SetXY(self.heroNamePos[1], self.heroNamePos[2] - pos);
        else
            self.barBlood:SetXY(self.bloodBarPos[1], self.bloodBarPos[2]);
            self.barEnergy:SetXY(self.energyPos[1], self.energyPos[2]);
            self.heroName:SetXY(self.heroNamePos[1], self.heroNamePos[2]);
        end
    end
end

function _M:refreshBattleHeroState()
    local hpValue,enValue;
    if self.realManPlayerInfo.myTeam[self.battlePartnerId] == nil then
        hpValue = 100;
        enValue = 100;
    else
        local battleHero = self.realManPlayerInfo.myTeam[self.battlePartnerId];
        if battleHero.hp == -1 then
            hpValue = 100;
        else
            hpValue = math.min(battleHero.hp / battleHero.maxHp * 100, 100);
        end
        enValue = math.min(tonumber(battleHero.energy) / 1000 * 100, 100);
    end
    self.barBlood:TweenValue(hpValue, 0.5);
    self.barEnergy:TweenValue(enValue, 0.5);
end

-- 加载敌方英雄
function _M:loadEnemyHero(modelId, isNextEnemy, isReward)
    local path = HeroManager.GetHeroDrawingRolePath(modelId);
    if isNextEnemy then
        --self.heroMovePos:Set(self.btnEnemy.x, self.btnEnemy.y);
        self.btnEnemy:SetXY(self.btnNextEnemy.x, self.btnNextEnemy.y);
    end
    --resMgr:LoadModelPrefab(path, function(go)
    --    if self.enemyRole ~= nil then
    --        destroyImmediate(self.enemyRole);
    --        self.enemyRole = nil
    --    end
    --    self.enemyRole = newObject(go);
    --    self.enemyRole:AddComponent(TCanvas)
    --    if self.enemyWrapper == nil then
    --        self.enemyWrapper = GoWrapper.New()
    --    end
    --    self.enemyWrapper:SetWrapTarget(self.enemyRole,false);
    --    Util.Lua_SetLocalPositionXYZ(self.enemyRole, 100, -365, 0);
    --    Util.Lua_SetLocalScaleXYZ(self.enemyRole, 150, 150, 150);
    --    --Util.Lua_SetLocalRotationXYZ(self.enemyRole, 0, 180, 0);
    --    if isReward ~= nil and isReward then
    --        self.shopNode:SetNativeObject(self.enemyWrapper);
    --    else
    --        self.enemyNode:SetNativeObject(self.enemyWrapper);
    --    end
    --    self.enemyObj = Util.Lua_FindGameObject(self.enemyRole, "Animation");
    --    Util.Lua_SetLocalRotationXYZ(self.enemyObj, 0, 0, 0);
    --    --self.skeletonAnim = self.enemyObj:GetComponent("SkeletonAnimation");
    --    self.enemyObj.transform.localEulerAngles = Vector3.New(0, 180, 0);
    --    local shadow = Util.Lua_FindGameObject(self.enemyRole, "shadow");
    --    if shadow ~= nil then
    --        shadow.transform.localEulerAngles = Vector3.New(40, 180, 0);
    --    end
    --    logicMgr.EquipManager.setWeaponSkin(self.enemyObj, modelId, true);
    --    Util.PlayAction(self.enemyObj, self:getAnimationName(modelId,"idleBattle"));
    --    if isReward ~= nil and isReward then
    --        self:loadRewardHeroEnd();
    --    else
    --        self:loadEnd();
    --    end
    --end);
    if self.enemyHeroItem == nil then
        self.enemyHeroItem = self.SoloBattleHeroItem.New();
        self.enemyHeroItem:OnCreate(self.btnEnemy,self.attEff,self);
    end
    self.enemyHeroItem:OnShow(modelId, true, true);
end

-- 加载敌方第二个英雄
function _M:loadEnemyHero2(modelId, isNextEnemy)
    local path = HeroManager.GetHeroDrawingRolePath(modelId);
    if isNextEnemy then
        --self.heroMovePos2:Set(self.btnEnemy2.x, self.btnEnemy2.y);
        self.btnEnemy2:SetXY(self.btnNextEnemy2.x, self.btnNextEnemy2.y);
    end
    --resMgr:LoadModelPrefab(path, function(go)
    --    if self.enemyRole2 ~= nil then
    --        destroyImmediate(self.enemyRole2);
    --        self.enemyRole2 = nil
    --    end
    --    self.enemyRole2 = newObject(go);
    --    self.enemyRole2:AddComponent(TCanvas)
    --    if self.enemyWrapper2 == nil then
    --        self.enemyWrapper2 = GoWrapper.New()
    --    end
    --    self.enemyWrapper2:SetWrapTarget(self.enemyRole2, false);
    --    Util.Lua_SetLocalPositionXYZ(self.enemyRole2, 100, -365, 0);
    --    Util.Lua_SetLocalScaleXYZ(self.enemyRole2, 150, 150, 150);
    --
    --    self.enemyNode2:SetNativeObject(self.enemyWrapper2);
    --    self.enemyObj2 = Util.Lua_FindGameObject(self.enemyRole2, "Animation");
    --    Util.Lua_SetLocalRotationXYZ(self.enemyObj2, 0, 0, 0);
    --    self.enemyObj2.transform.localEulerAngles = Vector3.New(0, 180, 0);
    --    local shadow = Util.Lua_FindGameObject(self.enemyRole2, "shadow");
    --    if shadow ~= nil then
    --        shadow.transform.localEulerAngles = Vector3.New(40, 180, 0);
    --    end
    --    logicMgr.EquipManager.setWeaponSkin(self.enemyObj2, modelId, true);
    --    Util.PlayAction(self.enemyObj2, self:getAnimationName(modelId, "idleBattle"));
    --end);
    if self.enemyHeroItem2 == nil then
        self.enemyHeroItem2 = self.SoloBattleHeroItem.New();
        self.enemyHeroItem2:OnCreate(self.btnEnemy2, self.attEff, self);
    end
    self.enemyHeroItem2:OnShow(modelId, true, false);
end

function _M:updateEnemyInfo(isBattle)
    self.enemyHeros = {}
    if self.abyssCfg ~= nil then
        local monster = dataTable.getDataTableByStringId("t_monsterteam",self.abyssCfg.f_AbyssMonsterTeamId);
        if monster == nil then
            self.listEnemyHero.numItems = 0;
        else
            local occupation = monster.f_occupation;
            local hero = self:getEnemyHeroInfo(1, monster.f_MonsterPara1, occupation, isBattle);
            if hero ~= nil then
                table.insert(self.enemyHeros, hero);
            end
            hero = self:getEnemyHeroInfo(2, monster.f_MonsterPara2, occupation, isBattle);
            if hero ~= nil then
                table.insert(self.enemyHeros, hero);
            end
            hero = self:getEnemyHeroInfo(3, monster.f_MonsterPara3, occupation, isBattle);
            if hero ~= nil then
                table.insert(self.enemyHeros, hero);
            end
            hero = self:getEnemyHeroInfo(4, monster.f_MonsterPara4, occupation, isBattle);
            if hero ~= nil then
                table.insert(self.enemyHeros, hero);
            end
            hero = self:getEnemyHeroInfo(5, monster.f_MonsterPara5, occupation, isBattle);
            if hero ~= nil then
                table.insert(self.enemyHeros, hero);
            end
            hero = self:getEnemyHeroInfo(6, monster.f_MonsterPara6, occupation, isBattle);
            if hero ~= nil then
                table.insert(self.enemyHeros, hero);
            end
        end
    end
    self.listEnemyHero.numItems = #self.enemyHeros;
end

function _M:getEnemyHeroInfo(index, monsterPara, occupation, isBattle)
    if monsterPara ~= nil and monsterPara ~= "" then
        local hero = {};
        hero.modelId = monsterPara[1];
        hero.partnerId = tostring(index);
        local heroCfg = G.dataTable["t_hero"][tostring(hero.modelId)];
        hero.icon = heroCfg.f_HeroIcon;
        hero.lv = monsterPara[2];
        hero.star = monsterPara[4];
        hero.type = occupation[index];
        hero.deadType = 1;
        if self.realManPlayerInfo.armyTeam[hero.partnerId] == nil or self.realManPlayerInfo.armyTeam[hero.partnerId].hp == -1 then
            if isBattle and self.battleResult ~= nil and self.battleResult.fightResult == 1 then
                hero.hpValue = 0;
                hero.enValue = 0;
            else
                hero.hpValue = 100;
                hero.enValue = 0;
            end
        else
            local battleHero = self.realManPlayerInfo.armyTeam[hero.partnerId];
            hero.hpValue = math.min(battleHero.hp / battleHero.maxHp * 100, 100);
            hero.enValue = math.min(tonumber(battleHero.energy) / 1000 * 100, 100);
            if battleHero.hp == 0 then
                hero.deadType = 0;
            end
        end
        return hero;
    end
    return nil;
end

function _M.refreshItem(index, itemObj)
    local hero = _M.enemyHeros[index + 1];
    local quality = itemObj:GetChild("quality");
    local qualityBG = itemObj:GetChild("qualityBG");
    local icon = itemObj:GetChild("icon");
    local lv = itemObj:GetChild("level");
    local lvbg = itemObj:GetChild("n29");
    local typeIcon = itemObj:GetChild("Occupation");
    local typeShowController = itemObj:GetController("Occupation");
    local barCtrl = itemObj:GetController("Bar");
    local herodead = itemObj:GetController("herodead");
    local bloodBar = itemObj:GetChild("BloodBar");
    local energyBar = itemObj:GetChild("EnergyBar");
    local unActive = itemObj:GetChild("unActive");
    local choose = itemObj:GetChild("maskChoose");
    choose.visible = false;
    unActive.visible = false;
    typeShowController.selectedIndex = 0;
    barCtrl.selectedIndex = 1;
    local qua = logicMgr.HeroManager.starToQuality(hero["star"]);
    if qua == nil or qua == 0 then
        qua = 1;
    elseif qua > #Config.IconQuality then
        qua = #Config.IconQuality;
    end
    quality.visible = true;
    qualityBG.visible = true;
    quality.url = "ui://ICONAtlas/" .. Config.IconFrame[qua];
    qualityBG.url = "ui://ICONAtlas/" .. Config.IconQuality[qua];
    icon.visible = true;
    icon:GetChild("icon").url = "ui://ICONAtlas/" .. hero.icon;
    lv.visible = true;
    lvbg.visible = true;
    lv.text = string.format(getLanguage("Level"), hero.lv);
    typeIcon.url = "ui://ICONAtlas/" .. EnumConst.HeroTypeIcon[hero.type];
    bloodBar.value = hero.hpValue;
    energyBar.value = hero.enValue;
    herodead.selectedIndex = 0;
    if hero.hpValue == 0 then
        herodead.selectedIndex = 1;
    end
end

-- 加载攻击特效
function _M:loadSkillEffect(heroId, isEnemy)
    --local heroCfg = G.dataTable["t_hero"][tostring(heroId)];
    --if heroCfg ~= nil then
    --    local roleCfg = G.dataTable["t_roleconfig"][tostring(heroCfg.f_HeroModel)];
    --    if roleCfg ~= nil then
    --        local skills = roleCfg.nomalSkill--decodeJsonStrToTabel(roleCfg.nomalSkill);
    --        local skillId = skills[1];
    --        local skillData = G.dataTable["t_skill"][tostring(skillId)];
    --        local skillShow = G.dataTable["t_skillshow"][tostring(skillId)];
    --
    --        if skillShow.f_SkillFResource ~= nil and skillShow.f_SkillFResource ~= "" and #skillShow.f_SkillFResource > 0 then
    --            if isEnemy then
    --                if self.enemyDeadIndex == 0 or self.enemyDeadIndex == 2 or not self.realManPlayerInfo.needLoadHero then
    --                    self.attEff:SetXY(self.btnEnemy.x, self.btnEnemy.y);
    --                else
    --                    self.attEff:SetXY(self.btnEnemy2.x, self.btnEnemy2.y);
    --                end
    --            else
    --                self.attEff:SetXY(self.btnHero.x, self.btnHero.y);
    --            end
    --
    --            resMgr:LoadModelPrefab("Prefabs/Skill/" .. skillShow.f_SkillFResource[1], function(go)
    --                if self.effectGo ~= nil then
    --                    destroyImmediate(self.effectGo);
    --                    self.effectGo = nil
    --                end
    --                self.effectGo = newObject(go);
    --                self.effectGo:AddComponent(TCanvas);
    --                if self.effectWrapper == nil then
    --                    self.effectWrapper = GoWrapper.New();
    --                end
    --                self.effectWrapper:SetWrapTarget(self.effectGo,false);
    --                Util.Lua_SetLocalPositionXYZ(self.effectGo, 100, -365, 0);
    --                Util.Lua_SetLocalScaleXYZ(self.effectGo, 150, 150, 150);
    --                if isEnemy then
    --                    Util.Lua_SetLocalRotationXYZ(self.effectGo, 0, 180, 0);
    --                end
    --                self.attEffNode:SetNativeObject(self.effectWrapper);
    --                local effectObj = Util.Lua_FindGameObject(self.effectGo, "Animation");
    --                --if isEnemy then
    --                --    effectObj.transform.localEulerAngles = Vector3.New(0, 180, 0);
    --                --end
    --                Util.PlayAction(effectObj, skillShow.f_SkillFName, false);
    --                self:playHeroAttack(isEnemy);
    --            end);
    --        else
    --            self:playHeroAttack(isEnemy);
    --        end
    --    else
    --        self:playHeroAttack(isEnemy);
    --    end
    --else
    --    self:playHeroAttack(isEnemy);
    --end
end

-- 加载受击特效
function _M:loadHitEffect(heroId, isEnemy)
    local heroCfg = G.dataTable["t_hero"][tostring(heroId)];
    if heroCfg ~= nil then
        local roleCfg = G.dataTable["t_roleconfig"][tostring(heroCfg.f_HeroModel)];
        if roleCfg ~= nil then
            local skills = roleCfg.nomalSkill--decodeJsonStrToTabel(roleCfg.nomalSkill);
            local skillId = skills[1];
            local skillData = G.dataTable["t_skill"][tostring(skillId)];
            local skillShow = G.dataTable["t_skillshow"][tostring(skillId)];

            if skillShow.f_HitFResource == nil or skillShow.f_HitFResource == "" and skillData.f_SkillBySkill ~= nil and skillData.f_SkillBySkill ~= "" then
                skillId = skillData.f_SkillBySkill[1][2];
                skillData = G.dataTable["t_skill"][tostring(skillId)];
                skillShow = G.dataTable["t_skillshow"][tostring(skillId)];
            end

            if skillShow and skillData ~= nil then
                if skillShow.f_HitFResource ~= nil and skillShow.f_HitFResource ~= "" then
                    local effRes;
                    if type(skillShow.f_HitFResource) == "table" then
                        effRes = skillShow.f_HitFResource[1];
                    else
                        effRes = skillShow.f_HitFResource;
                    end
                    if isEnemy then
                        self.hitEff:SetXY(self.btnHero.x, self.btnHero.y);
                    else
                        self.hitEff:SetXY(self.btnEnemy.x, self.btnEnemy.y);
                    end

                    resMgr:LoadModelPrefab("Prefabs/Skill/" .. effRes, function(go)
                        if self.hitEffectGo ~= nil then
                            destroyImmediate(self.hitEffectGo);
                            self.hitEffectGo = nil
                        end
                        self.hitEffectGo = newObject(go);
                        self.hitEffectGo:AddComponent(TCanvas);
                        if self.hitEffectWrapper == nil then
                            self.hitEffectWrapper = GoWrapper.New();
                        end
                        self.hitEffectWrapper:SetWrapTarget(self.hitEffectGo,false);
                        if skillShow.f_HitFPosition ~= nil and skillShow.f_HitFPosition ~= "" and #skillShow.f_HitFPosition >=3 then
                            Util.Lua_SetLocalPositionXYZ(self.hitEffectGo, 65, -180, 0);
                        else
                            Util.Lua_SetLocalPositionXYZ(self.hitEffectGo, 65, -365, 0);
                        end
                        Util.Lua_SetLocalScaleXYZ(self.hitEffectGo, 150, 150, 150);
                        self.hitEffNode:SetNativeObject(self.hitEffectWrapper);
                        local effectObj = Util.Lua_FindGameObject(self.hitEffectGo, "Animation");
                        Util.PlayAction(effectObj, skillShow.f_HitFName, false);
                    end);
                end
            end
        end
    end
end

--上阵特效
function _M:loadbuzhenEffect()
    resMgr:LoadModelPrefab("Prefabs/Skill/fxh104", function(go)
        if self.buzhenEffectGo ~= nil then
            destroyImmediate(self.buzhenEffectGo);
            self.buzhenEffectGo = nil
        end
        self.buzhenEffectGo = newObject(go);
        self.buzhenEffectGo:AddComponent(TCanvas);
        if self.buzhenEffectWrapper == nil then
            self.buzhenEffectWrapper = GoWrapper.New();
        end
        self.buzhenEffectWrapper:SetWrapTarget(self.buzhenEffectGo, false);
        Util.Lua_SetLocalPositionXYZ(self.buzhenEffectGo, 100, -365, 0);
        Util.Lua_SetLocalScaleXYZ(self.buzhenEffectGo, 60, 60, 60);
        self.buzhenEffectNode:SetNativeObject(self.buzhenEffectWrapper);
        self.buzhenEffectAnim = Util.Lua_FindGameObject(self.buzhenEffectGo, "Animation");
        Util.PlayAction(self.buzhenEffectAnim, "idle2", false);
    end);
end

function _M:playbuzhenEffect()
    if self.buzhenEffectAnim ~= nil then
        Util.PlayAction(self.buzhenEffectAnim, "idle2", false);
    else
        self:loadbuzhenEffect();
    end
end

function _M:loadEnd()
    self.loadNum = self.loadNum + 1;
    self.isEndsInDraw = false;
    if self.loadNum >= 3 then
        self.loadNum = 0;
        if self.battleResult ~= nil then
            self.isPlay = true;
            local modelId;
            if self.battleResult.fightResult == 1 then
                self.isEnemy = false;
                modelId = self.modelId;
                logicMgr.RealManWarManager.setPosIndex(self.curPosIndex + 1);
                if self.heroItem ~= nil then
                    self.heroItem:loadSkillEffect(modelId, false, true);
                end
            else
                if self.autoFightCtrl.selectedIndex == 1 then
                    self:onBtnAuto();
                end
                self.isPlayerOperation = true;
                if self.battlePartnerId ~= "" and self.battlePartnerId ~= "0" and self.battlePartnerId ~= "noHero" then
                    --判断是否平局
                    local team = self.realManPlayerInfo.myTeam[tostring(self.battlePartnerId)];
                    if team ~= nil and team.hp ~= 0 then
                        -- 平局
                        self.isEndsInDraw = true;
                        self.heroItem:loadSkillEffect(modelId, false, false);
                    end
                end

                self.isEnemy = true;
                if self.enemyHeros[1] ~= nil and self.enemyHeros[1].deadType == 1 then
                    modelId = self.enemyHeros[1].modelId;
                    if self.enemyHeroItem ~= nil then
                        self.enemyHeroItem:loadSkillEffect(modelId, true, true);
                    end
                    if self.enemyHeros[2] ~= nil and self.enemyHeros[2].deadType == 1 then
                        modelId = self.enemyHeros[2].modelId;
                        if self.enemyHeroItem2 ~= nil then
                            self.enemyHeroItem2:loadSkillEffect(modelId, true, false);
                        end
                    end
                else
                    if self.enemyHeros[2] ~= nil and self.enemyHeros[2].deadType == 1 then
                        modelId = self.enemyHeros[2].modelId;
                        if self.enemyHeroItem2 ~= nil and self.enemyHeroItem2:getHero() ~= nil then
                            self.enemyHeroItem2:loadSkillEffect(modelId, true, true);
                        else
                            if self.enemyHeroItem ~= nil and self.enemyHeroItem:getHero() ~= nil then
                                self.enemyHeroItem:loadSkillEffect(modelId, true, true);
                            end
                        end
                    end
                end
            end
            --self:loadSkillEffect(modelId, self.isEnemy);
        else
            if logicMgr.RealManWarManager.hasSweepReward() then
                logicMgr.RealManWarManager.setSweepReward(false);
                local rewardItem = self.realManPlayerInfo.items;
                if rewardItem ~= nil then
                    local itemNum = #rewardItem;
                    if itemNum > 0 then
                        local rewards = {};
                        for i = 1, itemNum do
                            local item = rewardItem[i];
                            local itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(item.id, item.num);
                            if itemData ~= nil then
                                table.insert(rewards, itemData);
                            end
                        end
                        if #rewards > 0 then
                            local str;
                            local cfgId = tonumber(self.realManPlayerInfo.step);
                            local ssCfg = G.dataTable["t_abyss"][tostring(cfgId)];
                            if ssCfg ~= nil then
                                str = string.format(getLanguage("SoloBattle_ClearTips"), string.format(getLanguage("SoloBattle_RankLevel"), string.format(getLanguage("SoloBattle_Level"), getLanguage("SoloBattle_difficulty0" .. ssCfg.f_Rank), ssCfg.f_RankLevel)));
                            else
                                str = string.format(getLanguage("SoloBattle_ClearTips"), "");
                            end
                            showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards, nil, str, nil, nil, nil, nil, 6);
                        end
                    end
                end
            end
        end
        self.isLoad = false;
    end
end

function _M:loadRewardHeroEnd()
    self.showShopTimeId = self:TimeCall(function()
        showUI(PanelNames.UIJumpShop, function()
            self:playRewardBoxEnd();
        end)
    end, 1000, 1);
end

function _M:removeTimeCall()
    if self.playHitEffTimeId ~= nil then
        utimer.remove(self.playHitEffTimeId);
        self.playHitEffTimeId = nil;
    end
    if self.playDeathTimeId ~= nil then
        utimer.remove(self.playDeathTimeId);
        self.playDeathTimeId = nil;
    end
    if self.playIdleTimeId ~= nil then
        utimer.remove(self.playIdleTimeId);
        self.playIdleTimeId = nil;
    end
    if self.deathEndTimeId ~= nil then
        utimer.remove(self.deathEndTimeId);
        self.deathEndTimeId = nil;
    end

    if self.showShopTimeId~= nil then
        utimer.remove(self.showShopTimeId);
        self.showShopTimeId = nil;
    end

    if self.showItemGetTimeId ~= nil then
        utimer.remove(self.showItemGetTimeId);
        self.showItemGetTimeId = nil;
    end

    if self.showPotionTimeId ~= nil then
        utimer.remove(self.showPotionTimeId);
        self.showPotionTimeId = nil;
    end

    if self.updatePotionTimeId ~= nil then
        utimer.remove(self.updatePotionTimeId);
        self.updatePotionTimeId = nil;
    end

    if self.destroyBuffEffectTimeId ~= nil then
        utimer.remove(self.destroyBuffEffectTimeId);
        self.destroyBuffEffectTimeId = nil;
    end

    if self.resetEffTimeId ~= nil then
        utimer.remove(self.resetEffTimeId);
        self.resetEffTimeId = nil;
    end
end

function _M:removeCommonGetTimer()
    if self.closeCommonGetTimer ~= nil then
        utimer.remove(self.closeCommonGetTimer);
        self.closeCommonGetTimer = nil;
    end
end

function _M:playHeroAttack(isEnemy)
    local duration = 0;
    local enemyModelId = 0;
    --local fighter, fighter2, target, target2;
    --local fighterModelId, fighterModelId2, targetModelId, targetModelId2;
    if isEnemy then
        --fighter = self.enemyObj;
        --fighterModelId = self.enemyModelId;
        --target = self.heroObj;
        --targetModelId = self.modelId;
        if self.enemyHeros[1] ~= nil and self.enemyHeros[1].deadType == 1 then
            if self.enemyHeros[2] ~= nil and self.enemyHeros[2].deadType == 1 then
                if self.enemyHeroItem2 ~= nil then
                    self.enemyHeroItem2:playAttack();
                end
            end
            if self.enemyHeroItem ~= nil then
                duration = self.enemyHeroItem:playAttack();
            end
            enemyModelId = self.enemyHeros[1].modelId;
        else
            if self.enemyHeros[2] ~= nil and self.enemyHeros[2].deadType == 1 then
                if self.enemyHeroItem2 ~= nil and self.enemyHeroItem2:getHero() ~= nil then
                    duration = self.enemyHeroItem2:playAttack();
                else
                    if self.enemyHeroItem ~= nil and self.enemyHeroItem:getHero() ~= nil then
                        duration = self.enemyHeroItem:playAttack();
                    end
                end
                enemyModelId = self.enemyHeros[2].modelId;
            end
        end
        if self.isEndsInDraw then
            if self.heroItem ~= nil then
                self.heroItem:playAttack();
            end
        end
    else
        --fighter = self.heroObj;
        --fighterModelId = self.modelId;
        --target = self.enemyObj;
        --targetModelId = self.enemyModelId;
        if self.heroItem ~= nil then
            duration = self.heroItem:playAttack();
        end
        --duration = Util.PlayAction(fighter, self:getAnimationName(fighterModelId,"attack"), false);
    end
    self:removeTimeCall();
    --duration = Util.PlayAction(fighter, self:getAnimationName(fighterModelId,"attack"), false);

    self.playHitEffTimeId = self:TimeCall(function()
        if isEnemy then
            if self.heroItem ~= nil then
                self.heroItem:loadHitEffect(enemyModelId);
            end
            if self.isEndsInDraw then
                if self.enemyHeroItem2 ~= nil then
                    self.enemyHeroItem2:loadHitEffect(self.modelId);
                end
                if self.enemyHeroItem ~= nil then
                    self.enemyHeroItem:loadHitEffect(self.modelId);
                end
            end
        else
            if self.enemyHeroItem2 ~= nil then
                self.enemyHeroItem2:loadHitEffect(self.modelId);
            end
            if self.enemyHeroItem ~= nil then
                self.enemyHeroItem:loadHitEffect(self.modelId);
            end
        end
        --self:loadHitEffect(self.modelId, isEnemy);
    end, (duration / 3) * 1000, 1);

    if self.isEndsInDraw then
        self.playIdleTimeId = self:TimeCall(function()
            if self.enemyHeroItem ~= nil then
                self.enemyHeroItem:playIdle();
            end
            if self.enemyHeroItem2 ~= nil then
                self.enemyHeroItem2:playIdle();
            end
            if self.heroItem ~= nil then
                self.heroItem:playIdle();
            end
            self:destroyHitEffect();
        end, duration * 1000, 1);
        self.isPlay = false;
        self.isEndsInDraw = false;
        return;
    end

    self.playDeathTimeId = self:TimeCall(function()
        --local deathTime = Util.PlayAction(target, self:getAnimationName(targetModelId,"dead"), false);
        local deathTime = 0;
        if isEnemy then
            if self.heroItem ~= nil then
                deathTime = self.heroItem:playDead();
            end
            if self.realManPlayerInfo.hasArmyDead then
                if self.enemyDeadIndex == 1 then
                    if self.enemyHeroItem ~= nil and self.enemyHeroItem:getHero() ~= nil then
                        self.enemyHeroItem:playDead();
                    end
                end
                if self.enemyDeadIndex == 2 then
                    if self.enemyHeroItem2 ~= nil and self.enemyHeroItem2:getHero() ~= nil then
                        self.enemyHeroItem2:playDead();
                    end
                end
            end
        else
            if self.enemyHeroItem2 ~= nil and self.enemyHeroItem2:getHero() ~= nil then
                deathTime = self.enemyHeroItem2:playDead();
            end
            if self.enemyHeroItem ~= nil and self.enemyHeroItem:getHero() ~= nil then
                deathTime = self.enemyHeroItem:playDead();
            end
        end
        if not isEnemy then
            self:BattleWin();
            if self.battleResult.reward == nil or self.battleResult.reward.type ~= 1 then
                self.deathEndTimeId = self:TimeCall(function()
                    self:playRewardBoxEnd();
                end, deathTime * 1000, 1);
            end
        else
            self.deathEndTimeId = self:TimeCall(function()
                self:BattleFail();
            end, deathTime * 1000, 1);
        end
    end, (duration / 2) * 1000, 1);

    self.playIdleTimeId = self:TimeCall(function()
        if isEnemy then
            if self.enemyDeadIndex == 0 then
                if self.enemyHeroItem ~= nil then
                    self.enemyHeroItem:playIdle();
                end
                if self.enemyHeroItem2 ~= nil then
                    self.enemyHeroItem2:playIdle();
                end
            elseif self.enemyDeadIndex == 1 then
                if self.enemyHeroItem2 ~= nil and self.enemyHeroItem2:getHero() ~= nil then
                    self.enemyHeroItem2:playIdle();
                elseif not self.realManPlayerInfo.hasArmyDead then
                    if self.enemyHeroItem ~= nil and self.enemyHeroItem:getHero() ~= nil then
                        self.enemyHeroItem:playIdle();
                    end
                end
            else
                if self.enemyHeroItem ~= nil and self.enemyHeroItem:getHero() ~= nil then
                    self.enemyHeroItem:playIdle();
                end
            end
        else
            if self.heroItem ~= nil then
                self.heroItem:playIdle();
            end
        end
        self:destroyHitEffect();
    end, duration * 1000, 1);
end

function _M:playRewardBox()
    --self:destroyEnemy();
    if self.battleResult.reward ~= nil then
        local type = self.battleResult.reward.type;
        local id = self.battleResult.reward.id;
        local num = self.battleResult.reward.num;
        if type == 3 then
            --药水
            self:playPotionDrop(id);
        elseif type == 2 then
            --buff
            self:loadBuff(id);
        elseif type == 1 then
            --奖励
            self:PlayBoxEffect(id, num);
        end

        self.resetEffTimeId = self:TimeCall(function()
            if type == 1 then
                self.boxEffect:Stop();
            end
            self.resetEff:Play();
        end, 2000, 1);

        --self:playRewardBoxEnd();
        --商店
        --self.loadNum = 0;
        --self:loadEnemyHero(9, false, true);
    end
end

function _M:playPotionDrop(modelId)
    local img_Quality =  self.item:GetChild("quality");
    local img_Frame = self.item:GetChild("frame");
    local img_Icon = self.item:GetChild("icon");
    UIComponentInterface:SetItemAllById(img_Icon, img_Quality, img_Frame, modelId);
    if modelId == self.POTION_REVIVE and self.potionDrop1 ~= nil then
        self.potionDrop1:Play();
    elseif modelId == self.POTION_ENERGY and self.potionDrop2 ~= nil then
        self.potionDrop2:Play();
    elseif modelId == self.POTION_HP and self.potionDrop3 ~= nil then
        self.potionDrop3:Play();
    end
    self.showPotionTimeId = self:TimeCall(function()
        --self:playRewardBoxEnd();
    end, 300, 1);
    self.updatePotionTimeId = self:TimeCall(function()
        self:updatePotion(modelId);
    end, 1000, 1);
end

function _M:updatePotion(modelId)
    if self.realManPlayerInfo.potion ~= nil and self.realManPlayerInfo.potion[modelId] ~= nil then
        local num = self.realManPlayerInfo.potion[modelId];
        if modelId == self.POTION_REVIVE then
            self.potionNum01.text = tostring(num);
        elseif modelId == self.POTION_ENERGY then
            self.potionNum02.text = tostring(num);
        elseif modelId == self.POTION_HP then
            self.potionNum03.text = tostring(num);
        end
    end
end

function _M:loadBuff(modelId)
    local effRes;
    if modelId == 1 then
        effRes = "UI_buff_nlq_red";
    elseif modelId == 2 then
        effRes = "UI_buff_nlq_blue";
    elseif modelId == 3 then
        effRes = "UI_buff_nlq_green";
    end
    if effRes ~= nil then
        resMgr:LoadModelPrefab("UIEff/" .. effRes, function(go)
            if self.buffEffectGo ~= nil then
                destroyImmediate(self.buffEffectGo);
                self.buffEffectGo = nil
            end
            self.buffEffectGo = newObject(go);
            self.buffEffectGo:AddComponent(TCanvas);
            if self.buffEffectWrapper == nil then
                self.buffEffectWrapper = GoWrapper.New();
            end
            self.buffEffectWrapper:SetWrapTarget(self.buffEffectGo, false);
            Util.Lua_SetLocalPositionXYZ(self.buffEffectGo, 0, 0, 0);
            Util.Lua_SetLocalScaleXYZ(self.buffEffectGo, 1, 1, 1);
            local qiu = Util.Lua_FindGameObject(self.buffEffectGo, "qiu");
            if qiu ~= nil then
                Util.Lua_SetLocalScaleXYZ(qiu, 0.6, 0.6, 0.6);
            end
            local dise = Util.Lua_FindGameObject(self.buffEffectGo, "dise");
            if dise ~= nil then
                Util.Lua_SetLocalScaleXYZ(dise, 0.6, 0.6, 0.6);
            end
            local wenli = Util.Lua_FindGameObject(self.buffEffectGo, "wenli");
            if wenli ~= nil then
                Util.Lua_SetLocalScaleXYZ(wenli, 0.6, 0.6, 0.6);
            end
            self.buffNode:SetNativeObject(self.buffEffectWrapper);
            self:playBuffDrop(modelId);
        end);
    else
        self:playBuffDrop(modelId);
    end
end

function _M:playBuffDrop(modelId)
    if modelId == 1 and self.buffDrop1 ~= nil then
        self.buffDrop1:Play();
    elseif modelId == 2 and self.buffDrop2 ~= nil then
        self.buffDrop2:Play();
    elseif modelId == 3 and self.buffDrop3 ~= nil then
        self.buffDrop3:Play();
    end
    self.showPotionTimeId = self:TimeCall(function()
        --self:playRewardBoxEnd();
    end, 300, 1);
    self.updatePotionTimeId = self:TimeCall(function()
        self:updateBuff(modelId);
    end, 1100, 1);
    self.destroyBuffEffectTimeId = self:TimeCall(function()
        self:destroyBuff();
    end, 1500, 1);
end

function _M:updateBuff(modelId)
    if self.realManPlayerInfo.buff ~= nil and self.realManPlayerInfo.buff[modelId] ~= nil then
        local num = self.realManPlayerInfo.buff[modelId];
        local value = math.min(num / 30 * 100, 100);
        local scale = 1;
        if self.buffTypeCfg ~= nil then
             local cfg = self.buffTypeCfg[tostring(modelId)];
            if cfg ~= nil then
                value = math.min(num / tonumber(cfg.f_MaxBuffLayer) * 100, 100);
                scale = tonumber(cfg.f_BuffEffectShow);
            end
        end
        if modelId == 1 then
            self.barAtk:TweenValue(value, 0.5);
            self.atkNum.text = "+"..(num * scale).."%";
        elseif modelId == 2 then
            self.barDef:TweenValue(value, 0.5);
            self.defNum.text = "+"..(num * scale).."%";
        elseif modelId == 3 then
            self.barHp:TweenValue(value, 0.5);
            self.hpNum.text = "+"..(num * scale).."%";
        end
    end
end

function _M:PlayBoxEffect(id, num)
    if self.boxEffect ~= nil then
        self.boxEffect.ignoreEngineTimeScale = false;
        self.boxEffect:Play();

        self.showItemGetTimeId = self:TimeCall(function()
            self:showItemGet(id, num);
        end, 1700, 1);
    else
        self:showItemGet(id, num);
    end
end

function _M:showItemGet(id, num)
    local rewards = {};
    local itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(id, num);
    if itemData ~= nil then
        table.insert(rewards, itemData);
    end
    if #rewards > 0 then
        if self.isPlayerOperation then
            showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards, function()
                --self:removeCommonGetTimer();
                self:playRewardBoxEnd();
            end);
        else
            showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards, function()
                --self:removeCommonGetTimer();
                self:playRewardBoxEnd();
            end, nil, nil, nil, nil, nil, nil, nil, 2);
        end
        --self:removeCommonGetTimer();
        --self.closeCommonGetTimer = self:TimeCall(function()
        --    if isOpenUI(PanelNames.UICommonItemGet) then
        --        closeUI(PanelNames.UICommonItemGet);
        --    end
        --    self:removeCommonGetTimer();
        --    self:playRewardBoxEnd();
        --end, 3000, 1);
    else
        self:playRewardBoxEnd();
    end
end

function _M:playRewardBoxEnd()
    self:destroyEnemy();
    self:destroySkillEffect();
    local cfgId = tonumber(self.realManPlayerInfo.step) + 1;
    self.abyssCfg = G.dataTable["t_abyss"][tostring(cfgId)];
    self.enemyModelId = "";
    if self.abyssCfg ~= nil then
        self.enemyModelId = tostring(self.abyssCfg.f_HeroID);
    end
    if self.enemyModelId ~= nil and self.enemyModelId ~= "" then
        self.loadNum = 0;
        self:showTaskInfo(cfgId)
        self:updateEnemyInfo(false);
        if self.enemyHeros[1] ~= nil then
            self:loadEnemyHero(self.enemyHeros[1].modelId, true);
        end
        if self.enemyHeros[2] ~= nil then
            self:loadEnemyHero2(self.enemyHeros[2].modelId, true);
        end
        --Util.PlayAction(self.heroObj, self:getAnimationName(self.modelId, "run"));
        if self.heroItem ~= nil then
            self.heroItem:playRun();
        end
        self:moveMapAndHero();
    else
        -- 打穿了
        self.typeCtrl.selectedIndex = 2;
        self.textLevel.text = "";
        self.isPlay = false;
        --Util.PlayAction(self.heroObj, self:getAnimationName(self.modelId,"idleBattle"));
        if self.heroItem ~= nil then
            self.heroItem:playIdle();
        end
        self:hideTaskInfo()
    end
end

function _M:BattleWin()
    self:playRewardBox();
end

function _M:BattleFail()
    self:hideHeroInfo();
    self:destroySkillEffect();
    --判断是否有英雄存活
    self:setSurvivalHero();
    self:destroyPlayRole();
    if self.modelId == nil or self.modelId == "" then
        if self.realManPlayerInfo.potion ~= nil and self.realManPlayerInfo.potion[self.POTION_REVIVE] ~= nil and self.realManPlayerInfo.potion[self.POTION_REVIVE] > 0 then
            showUI(PanelNames.UIUsingPotion, nil, self.POTION_REVIVE);
        else
            --英雄全阵亡结束
            self.typeCtrl.selectedIndex = 1;
        end
        self.battlePartnerId = "noHero";
    else
        self.loadNum = 0;
        self.listHero.numItems = #self.realManPlayerInfo.formation;
        self:loadHero(self.modelId);
        self:playbuzhenEffect();
    end
    self.isPlay = false;
end

function _M:setSurvivalHero()
    self.modelId = "";
    self.battlePartnerId = "";
    local battleNum = #self.realManPlayerInfo.formation;
    for i = 1, battleNum do
        local parId = self.realManPlayerInfo.formation[i];
        if self.selectedHero[parId] ~= nil and (self.realManPlayerInfo.myTeam[parId] == nil or self.realManPlayerInfo.myTeam[parId].hp ~= 0) then
            self.battlePartnerId = parId;
            self.modelId = self.selectedHero[parId].partnerModelId;
            break;
        end
    end
end

function _M:moveMapAndHero()
    local tempVec = Vector3.New(0, 0, 0);
    local tempPos = Vector3.New(0, 0, 0);
    tempPos:Set(self.map.x, self.map.y);
    tempVec:Set(self.MapPos[self.curPosIndex + 1][1], self.MapPos[self.curPosIndex + 1][2]);

    tempPos:Set(tempPos.x - tempVec.x, tempPos.y - tempVec.y);
    local len = tempPos:Magnitude();
    local time = len / self.speed;
    if self.mapTween then
        self.mapTween:Kill();
        self.mapTween = nil;
    end
    --local heroPos = Vector3.New(0, 0, 0);
    --heroPos:Set(self.btnEnemy.x, self.btnEnemy.y);
    self:killTween();
    self.mapTween = self.map:TweenMove(tempVec, time):SetEase(EaseType.Linear):OnComplete(function()
        self.isPlay = false;
        if self.abyssCfg ~= nil then
            self.textLevel.text = string.format(getLanguage("SoloBattle_Level"), getLanguage("SoloBattle_difficulty0"..self.abyssCfg.f_Rank),self.abyssCfg.f_RankLevel);
        else
            self.textLevel.text = "";
        end
        if not self.isPlayerOperation then
            self:onBtnBattle();
        end
    end);
    --self.heroTween = self.btnNextEnemy:TweenMove(self.heroMovePos, time):SetEase(EaseType.Linear):OnComplete(function()
    --    Util.PlayAction(self.heroObj, self:getAnimationName(self.modelId,"idleBattle"));
    --end);
    self.heroTween = self.btnEnemy:TweenMove(self.heroMovePos, time):SetEase(EaseType.Linear):OnComplete(function()
        if self.heroItem ~= nil then
            self.heroItem:playIdle();
        end
    end);
    self.heroTween2 = self.btnEnemy2:TweenMove(self.heroMovePos2, time):SetEase(EaseType.Linear):OnComplete(function()

    end);
end

function _M:killTween()
    if self.heroTween then
        self.heroTween:Kill();
        self.heroTween = nil;
    end
    if self.heroTween2 then
        self.heroTween2:Kill();
        self.heroTween2 = nil;
    end
    self.textShow:Play(-1, 0, nil);
    self.autoCtrl:Play(-1, 0, nil);
end

function _M:getAnimationName(heroId, aniName)
    local heroInfo = dataTable.getRow("t_hero", heroId);
    local modelName;
    if heroInfo then
        modelName = heroInfo.f_HeroModel;
    end
    if modelName ~= nil then
        local str = G.dataTable["t_roleconfig"][modelName][aniName][1];
        local randomNames = string.split(str, ",");
        return randomNames[math.random(1, #randomNames)];
    end
    return "fidle";
end

-- 显示
function _M:OnEnable()
    Time.timeScale = 1
    self:addLocalEvent();
    self:closeAllUI();
    
    AudioManager.Play(30)
end

-- 隐藏
function _M:OnDisable()
    self:removeLocalEvent();
end

function _M:closeAllUI()
    closeUI(PanelResNames.UIHeadMenu);
    --if logicMgr.RealManWarManager.getOpenType() == 1 then
    --    closeUI(PanelNames.UIAdventureMain,true);
    --else
    --    closeUI(PanelNames.UIFuncMenu);
    --    closeUI(PanelNames.UIBattle);
    --end
end

function _M:closeRealManBattle()
    self:onClosePanel();
end

function _M:onClosePanel()
    if self.isLoad then
        return;
    end
    logicMgr.RealManWarManager.setFightResult(nil);
    logicMgr.RealManWarManager.setPlayerOperationState(false);
    closeUI(PanelNames.UISoloBattle);
    AudioManager.Play(7)

    showUI(PanelResNames.UIHeadMenu);
    --if logicMgr.RealManWarManager.getEnterBattle() then
    --    if logicMgr.RealManWarManager.getOpenType() == 1 then
    --        UIBottom.enterAdventure();
    --    else
    --        showUI(PanelNames.UIBattle);
    --        showUI(PanelNames.UIFuncMenu);
    --        UIBottom.onSetBattle();
    --    end
    --end
    UISpeed()
end

function _M:destroyEnemy()
    --if self.enemyRole ~= nil then
    --    destroy(self.enemyRole);
    --    self.enemyRole = nil;
    --end
    --if self.enemyRole2 ~= nil then
    --    destroy(self.enemyRole2);
    --    self.enemyRole2 = nil;
    --end
    --if self.enemyWrapper then
    --    self.enemyWrapper:Dispose();
    --    self.enemyWrapper = nil;
    --end
    --if self.enemyWrapper2 then
    --    self.enemyWrapper2:Dispose();
    --    self.enemyWrapper2 = nil;
    --end
    --self.enemyObj2 = nil;

    if self.enemyHeroItem ~= nil then
        self.enemyHeroItem:destroyEnemy();
    end
    if self.enemyHeroItem2 ~= nil then
        self.enemyHeroItem2:destroyEnemy();
    end
end

function _M:destroyPlayRole()
    --if self.playRole ~= nil then
    --    destroy(self.playRole);
    --    self.playRole = nil;
    --end
    --if self.heroWrapper then
    --    self.heroWrapper:Dispose();
    --    self.heroWrapper = nil;
    --end
    if self.heroItem ~= nil then
        self.heroItem:destroyEnemy();
    end
    if self.realManPlayerInfo.hasArmyDead then
        if self.enemyDeadIndex == 1 then
            if self.enemyHeroItem ~= nil and self.enemyHeroItem:getHero() ~= nil then
                self.enemyHeroItem:destroyEnemy();
            end
        end
        if self.enemyDeadIndex == 2 then
            if self.enemyHeroItem2 ~= nil and self.enemyHeroItem2:getHero() ~= nil then
                self.enemyHeroItem2:destroyEnemy();
            end
        end
    end
end

function _M:destroyBuff()
    if self.buffEffectGo ~= nil then
        destroy(self.buffEffectGo);
        self.buffEffectGo = nil;
    end
end

function _M:destroyHitEffect()
    if self.hitEffectGo ~= nil then
        destroy(self.hitEffectGo);
        self.hitEffectGo = nil;
    end

    if self.heroItem ~= nil then
        self.heroItem:destroyHitEffect();
    end
    if self.enemyHeroItem2 ~= nil and self.enemyHeroItem2:getHero() ~= nil then
        self.enemyHeroItem2:destroyHitEffect();
    end
    if self.enemyHeroItem ~= nil and self.enemyHeroItem:getHero() ~= nil then
        self.enemyHeroItem:destroyHitEffect();
    end
end

function _M:destroySkillEffect()
    if self.heroItem ~= nil then
        self.heroItem:destroySkillEffect();
    end
    if self.enemyHeroItem2 ~= nil and self.enemyHeroItem2:getHero() ~= nil then
        self.enemyHeroItem2:destroySkillEffect();
    end
    if self.enemyHeroItem ~= nil and self.enemyHeroItem:getHero() ~= nil then
        self.enemyHeroItem:destroySkillEffect();
    end
end

-- 卸载
function _M:OnDestroy()
    self.taskRewardDatas = nil
    self:removeEvent();
    self.hasInit = false;
    self:removeTimeCall();
    self:RemoveTimeCall(self.updateCountDown);
    --logicMgr.RealManWarManager.DestroyScene();

    if self.heroItem ~= nil then
        self.heroItem:destroyEnemy();
        self.heroItem = nil;
    end

    if self.enemyHeroItem ~= nil then
        self.enemyHeroItem:destroyEnemy();
        self.enemyHeroItem = nil;
    end

    if self.enemyHeroItem2 ~= nil then
        self.enemyHeroItem2:destroyEnemy();
        self.enemyHeroItem2 = nil;
    end

    self:removeCommonGetTimer();
    --self:destroyEnemy();
    --self:destroyPlayRole();
    if self.heroItem ~= nil then
        self.heroItem:OnDestroy();
    end
    self:destroyBuff();
    --self:destroyHitEffect();
    if self.mapPre ~= nil then
        destroy(self.mapPre);
        self.mapPre = nil;
    end
    if self.mapPreClone ~= nil then
        destroy(self.mapPreClone);
        self.mapPreClone = nil;
    end
    if self.effectGo ~= nil then
        destroy(self.effectGo);
        self.effectGo = nil;
    end

    if self.buzhenEffectGo ~= nil then
        destroy(self.buzhenEffectGo);
        self.buzhenEffectGo = nil;
    end

    if self.mapWrapper then
        self.mapWrapper:Dispose();
        self.mapWrapper = nil;
    end
    if self.mapWrapperC then
        self.mapWrapperC:Dispose();
        self.mapWrapperC = nil;
    end
    if self.effectWrapper then
        self.effectWrapper:Dispose();
        self.effectWrapper = nil;
    end
    if self.hitEffectWrapper then
        self.hitEffectWrapper:Dispose();
        self.hitEffectWrapper = nil;
    end
    if self.buzhenEffectWrapper then
        self.buzhenEffectWrapper:Dispose();
        self.buzhenEffectWrapper = nil;
    end
    if self.buffEffectWrapper then
        self.buffEffectWrapper:Dispose();
        self.buffEffectWrapper = nil;
    end

    if self.mapTween then
        self.mapTween:Kill();
        self.mapTween = nil;
    end
    if self.heroTween then
        self.heroTween:Kill();
        self.heroTween = nil;
    end
    if self.heroTween2 then
        self.heroTween2:Kill();
        self.heroTween2 = nil;
    end

    self.enemyObj = nil;
    self.heroObj = nil;
    self.isAutoBattle = false;
    self.battlePartnerId = "";
    self.isEndsInDraw = false;
    self.isPlayerOperation = true;
end



-- 英雄
_M.SoloBattleHeroItem = Class("SoloBattleHeroItem",UIComponentInterface)
local _M_T = _M.SoloBattleHeroItem
function _M_T:OnCreate(obj, attObj, parent)
    self.parent = parent;
    self.obj = obj;
    self.heroNode = obj:GetChild("Graph_Hero");
    self.effectNode = obj:GetChild("Graph_fxh104");
    self.enemyNode = obj:GetChild("Graph_Enemy");
    self.attEffNode = attObj:GetChild("Graph_fxh104");
    self.enemyRole = nil;
    self.modelName = nil;
end
function _M_T:OnShow(modelId, isEnemy, needBack)
    self.modelId = modelId;
    local path;
    if isEnemy then
        path = HeroManager.GetHeroDrawingRolePath(modelId);
    else
        local useSkinId = logicMgr.SkinManager.getHeroSkinId(modelId)
        path = logicMgr.SkinManager.GetHeroModelPathBySkinId(useSkinId);
    end

    resMgr:LoadModelPrefab(path, function(go)
        if self.enemyRole ~= nil then
            destroyImmediate(self.enemyRole);
            self.enemyRole = nil
        end
        self.enemyRole = newObject(go);
        self.enemyRole.name = path;
        self.enemyRole:AddComponent(TCanvas)
        if self.enemyWrapper == nil then
            self.enemyWrapper = GoWrapper.New()
        end
        self.enemyWrapper:SetWrapTarget(self.enemyRole,false);
        Util.Lua_SetLocalPositionXYZ(self.enemyRole, 100, -365, 0);
        Util.Lua_SetLocalScaleXYZ(self.enemyRole, 150, 150, 150);
        self.enemyObj = Util.Lua_FindGameObject(self.enemyRole, "Animation");
        Util.Lua_SetLocalRotationXYZ(self.enemyObj, 0, 0, 0);
        if isEnemy then
            self.enemyNode:SetNativeObject(self.enemyWrapper);
            self.enemyObj.transform.localEulerAngles = Vector3.New(0, 180, 0);
        else
            self.heroNode:SetNativeObject(self.enemyWrapper);
            self.enemyObj.transform.localEulerAngles = Vector3.New(0, 0, 0);
        end
        local shadow = Util.Lua_FindGameObject(self.enemyRole, "shadow");
        if shadow ~= nil then
            shadow.transform.localEulerAngles = Vector3.New(40, 180, 0);
        end
        logicMgr.EquipManager.setWeaponSkin(self.enemyObj, modelId, true);
        Util.PlayAction(self.enemyObj, self:getAnimationName(modelId,"idleBattle"));

        if needBack ~= nil and needBack then
            self.parent:loadEnd();
        end
    end);
end

-- 加载攻击特效
function _M_T:loadSkillEffect(heroId, isEnemy, needBack)
    if self.enemyRole == nil then
        return;
    end
    local heroCfg = G.dataTable["t_hero"][tostring(heroId)];
    if heroCfg ~= nil then
        local roleCfg = G.dataTable["t_roleconfig"][tostring(heroCfg.f_HeroModel)];
        if roleCfg ~= nil then
            local skills = roleCfg.nomalSkill--decodeJsonStrToTabel(roleCfg.nomalSkill);
            local skillId = skills[1];
            local skillData = G.dataTable["t_skill"][tostring(skillId)];
            local skillShow = G.dataTable["t_skillshow"][tostring(skillId)];

            if skillShow.f_SkillFResource ~= nil and skillShow.f_SkillFResource ~= "" and #skillShow.f_SkillFResource > 0 then
                resMgr:LoadModelPrefab("Prefabs/Skill/" .. skillShow.f_SkillFResource[1], function(go)
                    if self.effectGo ~= nil then
                        destroyImmediate(self.effectGo);
                        self.effectGo = nil
                    end
                    self.effectGo = newObject(go);
                    self.effectGo:AddComponent(TCanvas);
                    if self.effectWrapper == nil then
                        self.effectWrapper = GoWrapper.New();
                    end
                    self.effectWrapper:SetWrapTarget(self.effectGo,false);
                    Util.Lua_SetLocalPositionXYZ(self.effectGo, 100, -365, 0);
                    Util.Lua_SetLocalScaleXYZ(self.effectGo, 150, 150, 150);
                    if isEnemy then
                        Util.Lua_SetLocalRotationXYZ(self.effectGo, 0, 180, 0);
                        self.effectNode:SetNativeObject(self.effectWrapper);
                    else
                        Util.Lua_SetLocalRotationXYZ(self.effectGo, 0, 0, 0);
                        self.attEffNode:SetNativeObject(self.effectWrapper);
                    end
                    local effectObj = Util.Lua_FindGameObject(self.effectGo, "Animation");
                    Util.PlayAction(effectObj, skillShow.f_SkillFName, false);
                    if needBack ~= nil and needBack then
                        self.parent:playHeroAttack(isEnemy);
                    end
                end);
            else
                if needBack ~= nil and needBack then
                    self.parent:playHeroAttack(isEnemy);
                end
            end
        else
            if needBack ~= nil and needBack then
                self.parent:playHeroAttack(isEnemy);
            end
        end
    else
        if needBack ~= nil and needBack then
            self.parent:playHeroAttack(isEnemy);
        end
    end
end

-- 加载受击特效
function _M_T:loadHitEffect(heroId, isEnemy)
    if self.enemyRole == nil then
        return;
    end
    local heroCfg = G.dataTable["t_hero"][tostring(heroId)];
    if heroCfg ~= nil then
        local roleCfg = G.dataTable["t_roleconfig"][tostring(heroCfg.f_HeroModel)];
        if roleCfg ~= nil then
            local skills = roleCfg.nomalSkill--decodeJsonStrToTabel(roleCfg.nomalSkill);
            local skillId = skills[1];
            local skillData = G.dataTable["t_skill"][tostring(skillId)];
            local skillShow = G.dataTable["t_skillshow"][tostring(skillId)];

            if skillShow.f_HitFResource == nil or skillShow.f_HitFResource == "" and skillData.f_SkillBySkill ~= nil and skillData.f_SkillBySkill ~= "" then
                skillId = skillData.f_SkillBySkill[1][2];
                skillData = G.dataTable["t_skill"][tostring(skillId)];
                skillShow = G.dataTable["t_skillshow"][tostring(skillId)];
            end

            if skillShow and skillData ~= nil then
                if skillShow.f_HitFResource ~= nil and skillShow.f_HitFResource ~= "" then
                    local effRes;
                    if type(skillShow.f_HitFResource) == "table" then
                        effRes = skillShow.f_HitFResource[1];
                    else
                        effRes = skillShow.f_HitFResource;
                    end
                    resMgr:LoadModelPrefab("Prefabs/Skill/" .. effRes, function(go)
                        if self.hitEffectGo ~= nil then
                            destroyImmediate(self.hitEffectGo);
                            self.hitEffectGo = nil
                        end
                        self.hitEffectGo = newObject(go);
                        self.hitEffectGo:AddComponent(TCanvas);
                        if self.hitEffectWrapper == nil then
                            self.hitEffectWrapper = GoWrapper.New();
                        end
                        self.hitEffectWrapper:SetWrapTarget(self.hitEffectGo,false);
                        if skillShow.f_HitFPosition ~= nil and skillShow.f_HitFPosition ~= "" and #skillShow.f_HitFPosition >=3 then
                            Util.Lua_SetLocalPositionXYZ(self.hitEffectGo, 65, -180, 0);
                        else
                            Util.Lua_SetLocalPositionXYZ(self.hitEffectGo, 65, -365, 0);
                        end
                        Util.Lua_SetLocalScaleXYZ(self.hitEffectGo, 150, 150, 150);
                        self.effectNode:SetNativeObject(self.hitEffectWrapper);
                        local effectObj = Util.Lua_FindGameObject(self.hitEffectGo, "Animation");
                        Util.PlayAction(effectObj, skillShow.f_HitFName, false);
                    end);
                end
            end
        end
    end
end

function _M_T:getHero()
    return self.enemyRole;
end

function _M_T:playAttack()
    if self.enemyObj == nil then
        return 0;
    end
    return Util.PlayAction(self.enemyObj, self:getAnimationName(self.modelId,"attack"), false, -1, true);
end

function _M_T:playDead()
    if self.enemyObj == nil then
        return 0;
    end
    return Util.PlayAction(self.enemyObj, self:getAnimationName(self.modelId,"dead"), false);
end

function _M_T:playIdle()
    if self.enemyObj == nil then
        return 0;
    end
    return Util.PlayAction(self.enemyObj, self:getAnimationName(self.modelId,"idleBattle"));
end

function _M_T:playRun()
    if self.enemyObj == nil then
        return 0;
    end
    return Util.PlayAction(self.enemyObj, self:getAnimationName(self.modelId,"run"));
end

function _M_T:getAnimationName(heroId, aniName)
    if self.modelName == nil then
        local heroInfo = dataTable.getRow("t_hero", heroId);
        self.modelName = nil;
        if heroInfo then
            self.modelName = heroInfo.f_HeroModel;
        end
    end

    if self.modelName ~= nil then
        local str = G.dataTable["t_roleconfig"][self.modelName][aniName][1];
        local randomNames = string.split(str, ",");
        return randomNames[math.random(1, #randomNames)];
    end
    return "fidle";
end

function _M_T:destroyEnemy()
    if self.enemyRole ~= nil then
        destroy(self.enemyRole);
        self.enemyRole = nil;
    end
    self.enemyObj = nil;
    self.modelName = nil;
end

function _M_T:destroySkillEffect()
    if self.effectGo ~= nil then
        destroy(self.effectGo);
        self.effectGo = nil;
    end
end

function _M_T:destroyHitEffect()
    if self.hitEffectGo ~= nil then
        destroy(self.hitEffectGo);
        self.hitEffectGo = nil;
    end
end

function _M_T:OnDestroy()
    self:destroyEnemy();
    if self.enemyWrapper then
        self.enemyWrapper:Dispose();
        self.enemyWrapper = nil;
    end
    self:destroySkillEffect();
    if self.effectWrapper then
        self.effectWrapper:Dispose();
        self.effectWrapper = nil;
    end
    self:destroyHitEffect();
    if self.hitEffectWrapper then
        self.hitEffectWrapper:Dispose();
        self.hitEffectWrapper = nil;
    end
end

--------------------------------------------------------------------------------------------
--- 里程碑相关 BEGIN
--------------------------------------------------------------------------------------------

--- 任务数据结构
---@alias TaskStruct {id: number, rewards: number[][], progress: number}

--- 初始化里程碑奖励数据
---@private
function _M:initTaskRewardDatas()
    ---@type TaskStruct[]
    self.taskRewardDatas = {}
    
    local cfgTask = G.dataTable["t_task"]
    if cfgTask == nil then
        return
    end

    for k, v in pairs(cfgTask) do
        if v.f_TaskType == 25001 then
            local data = {
                id = tonumber(v.f_TaskId),
                rewards = decodeJsonStrToTabel(v.f_Award),
                progress = tonumber(v.f_Progress)
            }
            table.insert(self.taskRewardDatas, data)
        end
    end
    table.sort(self.taskRewardDatas, function(a, b)
        return a.id < b.id
    end)
end
--- 获取当前里程碑数据
---@private
---@param stageId number 当前通天塔关卡id
---@return TaskStruct 里程碑数据
function _M:getCurrTaskData(stageId)
    if not stageId or stageId == 0 then
        return self.taskRewardDatas[1] or {}
    end

    for i = 1, #self.taskRewardDatas do
        local taskData = self.taskRewardDatas[i]
        if taskData.progress >= stageId then
            return taskData
        end
    end

    return {}
end
--- 显示里程碑相关UI
---@private
---@param stageId number 当前通天塔关卡id
function _M:showTaskInfo(stageId)
    if not self.txtTaskDesc or not self.listTaskRewards then return end
    self.txtTaskDesc.visible = true
    self.listTaskRewards.visible = true

    -- 当前最高进度
    local maxStage = logicMgr.TaskManager.getProgressByType(25001) + 1
    maxStage = math.max(maxStage, stageId)

    -- 里程碑数据
    local taskData = self:getCurrTaskData(maxStage)
    if not taskData or not taskData.id then 
        self:hideTaskInfo()
        return
    end

    -- 关卡
    local cfgAbyss = G.dataTable["t_abyss"][tostring(taskData.progress)]
    if cfgAbyss ~= nil then
        self.txtTaskDesc.text = string.format(getLanguage("SoloBattle_Level"), getLanguage("SoloBattle_difficulty0" .. cfgAbyss.f_Rank), cfgAbyss.f_RankLevel)
    else
        self.txtTaskDesc.text = ""
    end

    -- 奖励
    -- local taskData = self:getCurrTaskData(stageId)
    logicMgr.CostManager.RefreshItemList(self.listTaskRewards, taskData.rewards, 100)
end
--- 隐藏里程碑相关UI
---@private
function _M:hideTaskInfo()
    if not self.txtTaskDesc or not self.listTaskRewards then return end
    self.txtTaskDesc.visible = false
    self.listTaskRewards.visible = false
end
--------------------------------------------------------------------------------------------
--- 里程碑相关 END
--------------------------------------------------------------------------------------------

return _M;